>> The problem is that DLLs have their own local heap for static variables.
Sorry - I still don't understand. Can you modify this code to demonstrate an "edge case"?
DLL 1:
Code:
#include <windows.h>
#include <iostream>
using namespace std;
int g_obj1;
extern "C" __declspec(dllexport)
void* GetSingleton1()
{
return &g_obj1;
}//GetSingleton1
extern "C" __declspec(dllexport)
void ReportInstances1()
{
HMODULE hmod = LoadLibraryA("DLL2.dll");
if (!hmod)
{
cout << "DLL1: LoadLibrary() failed, ec = " << GetLastError() << endl;
return;
}//if
typedef void* (*GetSingleton2_pfn)();
GetSingleton2_pfn GetSingleton2 = (GetSingleton2_pfn)GetProcAddress(hmod, "GetSingleton2");
if (!GetSingleton2)
{
cout << "DLL1: GetProcAddress() failed, ec = " << GetLastError() << endl;
FreeLibrary(hmod);
return;
}//if
cout << "DLL1: instance 1 at " << reinterpret_cast<long>(GetSingleton1()) << endl;
cout << "DLL1: instance 2 at " << reinterpret_cast<long>(GetSingleton2()) << endl;
FreeLibrary(hmod);
}//ReportInstances1
__declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD Reason, LPVOID Reserved)
{
return TRUE;
}//DllMain
DLL 2:
Code:
#include <windows.h>
#include <iostream>
using namespace std;
int g_obj2;
extern "C" __declspec(dllexport)
void* GetSingleton2()
{
return &g_obj2;
}//GetSingleton2
extern "C" __declspec(dllexport)
void ReportInstances2()
{
HMODULE hmod = LoadLibraryA("DLL1.dll");
if (!hmod)
{
cout << "DLL2: LoadLibrary() failed, ec = " << GetLastError() << endl;
return;
}//if
typedef void* (*GetSingleton1_pfn)();
GetSingleton1_pfn GetSingleton1 = (GetSingleton1_pfn)GetProcAddress(hmod, "GetSingleton1");
if (!GetSingleton1)
{
cout << "DLL2: GetProcAddress() failed, ec = " << GetLastError() << endl;
FreeLibrary(hmod);
return;
}//if
cout << "DLL2: instance 1 at " << reinterpret_cast<long>(GetSingleton1()) << endl;
cout << "DLL2: instance 2 at " << reinterpret_cast<long>(GetSingleton2()) << endl;
FreeLibrary(hmod);
}//ReportInstances2
__declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD Reason, LPVOID Reserved)
{
return TRUE;
}//DllMain
Main:
Code:
#include <windows.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "DLL1.lib")
#pragma comment(lib, "DLL2.lib")
extern "C" __declspec(dllimport) void* GetSingleton1();
extern "C" __declspec(dllimport) void* GetSingleton2();
extern "C" __declspec(dllimport) void ReportInstances1();
extern "C" __declspec(dllimport) void ReportInstances2();
int main()
{
cout << "MAIN: instance 1 at " << reinterpret_cast<long>(GetSingleton1()) << endl;
cout << "MAIN: instance 2 at " << reinterpret_cast<long>(GetSingleton2()) << endl;
ReportInstances1();
ReportInstances2();
return 0;
}//main
Output:
Code:
MAIN: instance 1 at 3421008
MAIN: instance 2 at 268448592
DLL1: instance 1 at 3421008
DLL1: instance 2 at 268448592
DLL2: instance 1 at 3421008
DLL2: instance 2 at 268448592
gg