Author Topic: BASS_Free hangs  (Read 182 times)

Miran

  • Posts: 12
BASS_Free hangs
« on: 23 Sep '24 - 20:20 »
BASS_Free function hangs up my application on exit. I tried to look for similar issues and found out the window handle given to the BASS_Init might be the cause. So instead of application's window I tried with NULL instead or with GetDesktopWindow. Still same behavior. After moving freeing call bit earlier to be extra sure that window still exists during BASS cleanup I can just see the window gets frozen.
Problem started after the application was reorganized into modules and audio engine along with all BASS related stuff now lives inside separate dll.
Interesting is Free successes when called right after BASS_Init.

Dump file:
https://www.file.io/jEY7/download/hok1NgxtAiBw

Ian @ un4seen

  • Administrator
  • Posts: 26157
Re: BASS_Free hangs
« Reply #1 on: 24 Sep '24 - 12:45 »
Are you calling BASS_Free in your DLL's DllMain function? If so, that isn't safe. KERNEL32.DLL is the only DLL that's really safe to call in there, and calling other DLLs can result in deadlocks/hangs (like in this case) or crashes. Here's some info on the subject:

   https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain
   https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices

Perhaps you can have the app call an uninitialization function in your DLL, and that can call BASS_Free? If that isn't possible then it may be best to not call BASS_Free at all and just let Windows release everything.

Miran

  • Posts: 12
Re: BASS_Free hangs
« Reply #2 on: 25 Sep '24 - 08:15 »
Bass_init is called by function triggered by some hook (when player is starting new/loading game).
Free is placed inside sound system's class destructor, so it happens when the dll is being unloaded.
Does both init and free has to be called from same thread or module?

Ok I will mess some more with that. Generally it happens in some specific conditions when game has additional mods installed.

Ian @ un4seen

  • Administrator
  • Posts: 26157
Re: BASS_Free hangs
« Reply #3 on: 25 Sep '24 - 13:15 »
BASS_Init and BASS_Free can be called in different threads and even modules, but not within a DllMain function. If it isn't possible to have an uninitialization function called before unloading the DLL then you could try just not calling BASS_Free.

Miran

  • Posts: 12
Re: BASS_Free hangs
« Reply #4 on: 30 Sep '24 - 08:51 »
Ok I tried to call free from different callbacks without success.
Not calling Bass_Free at all causes crashing for me (ModLoader puts it's injections everywhere).

Finally solution for problem with threads turned out to be just use more threads:
Code: [Select]
std::thread(BASS_Free);