Archive: Bug in BgImage


Bug in BgImage
I'm using BgImage, and was getting a crash, it seems the problem goes as follows:
BgImage::SetBg() is getting called *before* _DllMainCRTStartup ever gets called, and therefore is using a CriticalSection that was never initialized (using the debug build it simply crashes outright, the release one just sometimes crashes). Setting up a lazy init in ECS seems to have fixed the problem:


bool CriticalSectionInited=false;
CRITICAL_SECTION CriticalSection;

void ECS() {
if (!CriticalSectionInited) {
InitializeCriticalSection(&CriticalSection);
CriticalSectionInited = true;
}
EnterCriticalSection(&CriticalSection);
}

.
.
.

BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved) {
g_hInstance=hInst;
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
if (!CriticalSectionInited) {
InitializeCriticalSection(&CriticalSection);
CriticalSectionInited = true;
}
break;


I admit I know *nothing* about DLLs, so there might be something else going wrong here, it seems odd to me that you'd get a function called before getting an ATTACH message, but this seems to have fixed my problem. Thanks for the great plugin!

That's impossible.

Something else must be happening. Mind attaching the script and some more details about the tested OS?

Also, have you recompiled BgImage (before adding the patch)?


I am getting this behaviour in the Example.nsi that comes with BgImage (in the nsis.zip of recent CVS code). I simply built the debug version of BgImage.dll, copied it to my plugins/ folder, compiled the example, and it crashes immediately upon startup.

I'm running Windows XP (latest patches), and the crash that I seem to have fixed only exhibited itself on some laptops (the crash was happening in the Destroy code, I believe, so I built the debug version to find out where, but since the debug just died instantly I was unable to track it down. Now after this change, it's working fine).

If you are not getting this behaviour with the debug build let me know and I will try it on different computers to help track it down.


Ah... That's because the debug build is not configured right. It doesn't even call _DllMainCRTStartup in the debug version :)

I'll update that, thanks.

For now, just #define _DllMainCRTStartup as DllMain if _DEBUG is defined and it should work.


Ugh... well that means there's still a crashbug sitting in there somewhere... I'll let you know if I can get it to happen again...


Problem "solved", it was because SetBg was called from a section. Documentation updated to state it shouldn't be done.

Thanks Wasteland