次のコードについて明確にする必要があります。
次のコードでは、DLL からインターフェイスのプライベート実装を取得しています。
DLL がアンロードされた後 (CASE #2)、インターフェイスから取得した文字列とインターフェイス自体が両方とも無効になり、それらにアクセスするとアクセス違反が発生します (予期されます)。
しかし、私を混乱させているのは、DLL をリロードすると、DLL から再取得せずにインターフェイスと文字列を再度使用できることです。
これはこのように機能するはずですか?
メモリが無効になり、DLL が再度読み込まれると突然再び有効になるのはなぜですか?
これは運が良かっただけで、テスト プログラムが比較的単純なため、2 回目もメモリ内のまったく同じ場所に DLL がロードされるのでしょうか?
int main(int argc, int *argv[])
{
Interface *itf;
const char *name;
{
// loads the dll and gets functions
PersistenInterface pi("PersistentInterface.dll");
// returns a private implementation of the interface
itf = pi.CreateInterface();
name = itf->GetName();
// CASE #1
cout << name << endl; // suceeds
} // dll is unloaded in ~PersistenInterface()
// CASE #2
//cout << name << endl; // crashes
//cout << itf->GetName() << endl; // crashes
{
PersistenInterface pi("PersistentInterface.dll");
// CASE #3
cout << name << endl; // suceeds !?
cout << itf->GetName() << endl; // suceeds !?
}
return 0;
}