Archive: Problem with C++ class in plugin


Problem with C++ class in plugin
I have no errors when compiling:


...
TiXmlDocument doc;

extern "C" void __declspec(dllexport) LoadFile(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop)
{
...
if (!doc.LoadFile(szBuf))
...
}


But it crash when calling "doc.LoadFile(szBuf)".
If I write like this:

...
extern "C" void __declspec(dllexport) LoadFile(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop)
{
...
TiXmlDocument doc;
if (!doc.LoadFile(szBuf))
...
}

all working fine, but "TiXmlDocument doc" need to be global for other functions.

When I write console application no errors were occupied:

...
TiXmlDocument doc;

void loadit();
void parseit();
void saveit();

void main()
{
loadit();
parseit();
saveit();
}

void loadit()
{
...
if ( !doc.LoadFile(szBuf) )
...
}

Any suggestions?

Use /NOUNLOAD or give the user a handle. The global is destroyed when the DLL is unloaded.


I use /NOUNLOAD, but it is unimportant, because it is crash after first call "xml::LoadFile":


Section
xml::LoadFile /NOUNLOAD "demotest.xml" .r0
xml::Parse /NOUNLOAD .r0
xml::SaveFile .r0
SectionEnd

Then something else is probably happening inside xml::LoadFile. Not much can be said without more information.


Minimal variant of the strange error:


#include <windows.h>
#include "tinyxml.h"

typedef struct _stack_t {
struct _stack_t *next;
char text[1024];
} stack_t;

stack_t **g_stacktop;


TiXmlDocument doc;

extern "C" void __declspec(dllexport) Load(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop)
{
doc.LoadFile("demotest.xml");
}

BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}


Without error:

#include <windows.h>
#include "tinyxml.h"

typedef struct _stack_t {
struct _stack_t *next;
char text[1024];
} stack_t;

stack_t **g_stacktop;


extern "C" void __declspec(dllexport) Load(HWND hwndParent, int string_size,
char *variables, stack_t **stacktop)
{
TiXmlDocument doc;
doc.LoadFile("demotest.xml");
}

BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
{
return TRUE;
}

You need to manually initialize doc. Without libc, constructors for global variables aren't called.


I got it, thanks!


I had a little played with compiler options and here that have found out. I have erased entry point (/entry:"DllMain"->"") and no errors were occupied!