SDL2, DllMain, SDL_Init and Windows/RegisterClass locking up

Hi,

Using SDL2 in place of SDL1.2 on Windows/MinGW, I am experiencing
lockups. The code executed is the following (as shown by gdb under
mingw which I am using):

  1. application:__main
  2. libtcod.dll:DllMain
  3. SDL2.dll:SDL_Init
  4. SDL2.dll:SDL_HelperWindowCreate
  5. RegisterClass

I wouldn’t post about this, except for the following:

  • All googling matches about calling RegisterClass within DllMain
    (including StackOverflow) asserted that it should be safe.
  • This approach to initialising SDL within DLL has worked with no
    issue for SDL 1.2.

My naive assumption is that it is actually unsafe to call
RegisterClass within DllMain. I have confirmed this by removing the
libtcod.dll:DllMain initialisation, and ensured that it happens
appropriately in application:main, where the RegisterClass happens
successfully.

Any insight into this?

Cheers,
Richard.

I’m not sure whether it’s related but, according to MSDN:

"It is not safe to call LoadLibrary from DllMain. For more information,
see the Remarks section in DllMain."
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175(v=vs.85).aspx

What happens is that while LoadLibrary loads your library, it calls
DllMain(), if something there also calls LoadLibrary then it’s a
problem, as it seems that LoadLibrary is not recursively-safe (not
re-entrant).

You can google for “DllMain” “LoadLibrary” and more answers would come up.On 7/17/12 2:35 PM, Richard Tew wrote:

Hi,

Using SDL2 in place of SDL1.2 on Windows/MinGW, I am experiencing
lockups. The code executed is the following (as shown by gdb under
mingw which I am using):

  1. application:__main
  2. libtcod.dll:DllMain
  3. SDL2.dll:SDL_Init
  4. SDL2.dll:SDL_HelperWindowCreate
  5. RegisterClass

I wouldn’t post about this, except for the following:

  • All googling matches about calling RegisterClass within DllMain
    (including StackOverflow) asserted that it should be safe.
  • This approach to initialising SDL within DLL has worked with no
    issue for SDL 1.2.

My naive assumption is that it is actually unsafe to call
RegisterClass within DllMain. I have confirmed this by removing the
libtcod.dll:DllMain initialisation, and ensured that it happens
appropriately in application:main, where the RegisterClass happens
successfully.

Any insight into this?

Cheers,
Richard.