We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 1273

Summary: SDL_Init is crashing when called from DLL_main
Product: SDL Reporter: fojtik
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED WONTFIX QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2    
Version: 1.2.14   
Hardware: x86   
OS: Windows (XP)   

Description fojtik 2011-08-12 05:14:05 UTC
The code hangs. If I call SDL_Init(SDL_INIT_JOYSTICK) outdide DllMain, It does not crash.

----------------------------------------------------------

SDL_Joystick *joystick;
int JoystickNumAxes;

/** Main Windows .dll initialisation entry point. */
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  switch(fdwReason)
  {
    case DLL_PROCESS_ATTACH:
		{
	        if(SDL_Init(SDL_INIT_JOYSTICK)==0)
	        {
	          joystick = SDL_JoystickOpen(0);
		  JoystickNumAxes = SDL_JoystickNumAxes(joystick);
		}
		else
		  joystick = NULL;
		break;
		}
    case DLL_PROCESS_DETACH:		
	        if (joystick) 
                {
		   SDL_JoystickClose(joystick);	
		   joystick = NULL;
		}  //numAxes = numButtons = numHats = numTrackballs = 0;		
		break;
	
    //case DLL_THREAD_ATTACH:
    //case DLL_THREAD_DETACH:
  }
  return TRUE;
}
Comment 1 Sam Lantinga 2011-12-29 01:46:01 UTC
I suspect there's some problems on Windows with loading DLLs while a DLL is being loaded.
Comment 2 fojtik 2011-12-30 02:08:41 UTC
I have finally used native Windows joystick calls and do not use this library.

There is no other place to initialise joystick, only dll_main is available. 

The resulting DLL is intended to be a plugin for Matlab.