Reference becomes void on DLL unload


Hi All,

I am facing a weird issue here.

Problem:

I have a C++ dll and my C++ application is consuming it. One of the function of the DLL returns String as out parameter. Meaning I am taking String as a c++ reference and populating it in the function and expecting the populated value to be available to the caller. There is an issue in this implementation where the returned String object becomes void when we unload the DLL.

Debugging done:

We have debugged this issue and have found that whenever “Runtime Library” setting of the DLL project is set to “Multi-threaded Debug (/MTd)” the returned String becomes void if its size is greater than 7. If this setting is “Multi-threaded Debug DLL (/MDd)” then the returned reference remains valid even if we unload the DLL. At this point we are not sure whether the problem is with Microsoft or our implementation.

String has been defined as

****
typedef wchar_t StringChar;
typedef std::basic_string<StringChar> String;
****

Workaround:

If we set size of the string using String.resize then the String object remains valid even if the DLL project is set to “Multi-threaded Debug (/MTd)”

Can someone please advise me what might be going on here. (You can easily verify this even with a small test app.)

Source code of my DLL sample:

*****
#include "windows.h"
#include "iostream"
#include "TestDLL.h"

using namespace std;

BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;

case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

wstring MyString()
{
wstring str ( L"abcabcab" );
int i = str.capacity();
i = str.max_size();
return str;
}

int HelloWorld( wstring & str )
{
cout << "Hello World with param !";
int i = str.capacity();
i = str.max_size();
str = MyString();
return 0;
}
*****

Source code of my application:

*****
#include "windows.h"
#include "Winbase.h"
#include "TestDLL.h"
#include "iostream"

using namespace std;

void main()
{
typedef int (*DLLPROC) (wstring &);
HINSTANCE hinstDLL;
DLLPROC HelloWorld;
BOOL fFreeDLL;
wstring str1;
int i = str1.capacity();
i = str1.max_size();

abc a1;
a1.num = 5;

hinstDLL = LoadLibrary(L"C:\\TestCode\\DLL\\TestDLL\\Debug\\TestDLL.dll");
if (hinstDLL != NULL)
{
HelloWorld = (DLLPROC) GetProcAddress(hinstDLL, "HelloWorld");

if (HelloWorld != NULL)
{
int i = (HelloWorld)(str1);
}
else
{
DWORD dw = GetLastError();
cout << "Error in loading the DLL: " << dw;
}

fFreeDLL = FreeLibrary(hinstDLL);
cout << str1.c_str();
}
else
{
DWORD dw = GetLastError();
cout << "Error in loading the DLL: " << dw;
}
cout << str1.c_str();
}
*****

Thanks in advance,
Ganesh Tambat
closed account (S6k9GNh0)
use [code] tags
I take it you don't have a support contract with MS or they aren't responding, which is why you are posting here. In that case, regardless of where the issue is, don't bother waiting for MS to provide a fix.

Why do you need to unload the dll before the program has finished? Since, according to your analysis it only happens in this situation.
Topic archived. No new replies allowed.