std :: uncaught_exception()をチェックするデストラクタを持つDLLがあります。実行可能ファイルからtry/catch-blockで使用すると、例外がスローされた場合はtrueとは言えません。ここでstd :: uncaught_exceptionはDLLの境界を越えて動作しません
は、いくつかのサンプルコードです: lib.h:
#pragma once
class __declspec(dllexport) C final
{
public:
~C();
};
lib.cpp:
#include "lib.h"
#include <iostream>
#include <exception>
C::~C()
{
std::cout << "C says: Uncaught: " << (std::uncaught_exception() ? "yes" : "no") << std::endl;
}
main.cppに:
#include "lib.h"
#include <iostream>
#include <exception>
class D final
{
public:
~D()
{
std::cout << "D says: Uncaught: " << (std::uncaught_exception() ? "yes" : "no") << std::endl;
}
};
int main(int argc, char **argv)
{
try
{
C c;
D d;
throw 88;
}
catch (int a)
{
std::cout << "Code: " << a << std::endl;
}
{
C c;
D d;
}
return 0;
}
そして使用して、すべてを構築する:
私は結果を得るのVisual Studio 2015およびVisual Studio 2013のx64とx86で:私は2番目の行は
C says: Uncaught: yes
ことを期待する
D says: Uncaught: yes
C says: Uncaught: no
Code: 88
D says: Uncaught: no
C says: Uncaught: no
をだから、DLL内のクラスにはありません例外が発生してスタックの巻き戻しが発生し、デストラクタが呼び出されることを確認してください。しかし、内部クラスに直接位置するクラスはそれを見ます。
期待どおりに動作させるリンカー/コンパイラフラグはありますか?
BTW:ライブラリをビルドして使用するとき、dllexportとdllimportを区別しても結果は変わりません。 – mmmmmmmm
BTW2:std :: uncaught_exceptions()と同じです:1には0が与えられます。 – mmmmmmmm
私の推測では、dllのランタイムには、uncaught_exception状態を返す独自のフラグセットがあります。アプリのstd :: uncaught_exceptionの関数ポインタをdllに渡し、その関数ポインタを代わりに呼び出すと、期待される結果が得られるかもしれません。 –