From 8b66d2b96960d348481612ef74a45d4c2c4048fa Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sat, 22 May 2021 10:26:13 +0200 Subject: [PATCH] Delete critical section on DLL_PROCESS_DETACH (#1058) To explicitly release all allocated resources on shutdown on Windows, we delete the critical section on `DLL_PROCESS_DETACH`. We do not employ any locking to avoid any potential deadlock. In case of normal DLL unloading there is no need to, and in case of forced unloading all bets are likely off anyway. --- src/libsodium/sodium/core.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libsodium/sodium/core.c b/src/libsodium/sodium/core.c index ba875855..107d20c1 100644 --- a/src/libsodium/sodium/core.c +++ b/src/libsodium/sodium/core.c @@ -214,3 +214,16 @@ sodium_set_misuse_handler(void (*handler)(void)) } return 0; } + +#ifdef _WIN32 +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { + switch (fdwReason) { + case DLL_PROCESS_DETACH: + if (_sodium_lock_initialized == 2) { + DeleteCriticalSection(&_sodium_lock); + } + break; + } + return TRUE; +} +#endif