diff --git a/Src/envar.c b/Src/envar.c index 1992ee8..dfaba9b 100644 --- a/Src/envar.c +++ b/Src/envar.c @@ -46,11 +46,11 @@ PTCHAR StrAlloc(SIZE_T strlen) return (PTCHAR)GlobalAlloc(GPTR, strlen*sizeof(TCHAR)); } -/* Frees a string. */ -void StrFree(PTCHAR hVar) +/* Frees a string and clears the caller's pointer, so a second free is a no-op. */ +void StrFree(PTCHAR* phVar) { - GlobalFree(hVar); - hVar = NULL; + GlobalFree(*phVar); + *phVar = NULL; } /* Returns the string size. */ @@ -76,7 +76,7 @@ BOOL StrReAlloc(PTCHAR* phVar, int strlen) if (!temp) return 0; for (i = 0; i < StrSize(*phVar); i++) temp[i] = (*phVar)[i]; - StrFree(*phVar); + StrFree(phVar); *phVar = temp; return 1; @@ -93,9 +93,9 @@ void AllocStrs(void) /* Frees allocated buffers. */ void CleanUp(void) { - if (gptVarName) StrFree(gptVarName); - if (gptPathString) StrFree(gptPathString); - if (gptBuffer) StrFree(gptBuffer); + if (gptVarName) StrFree(&gptVarName); + if (gptPathString) StrFree(&gptPathString); + if (gptBuffer) StrFree(&gptBuffer); SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, (WPARAM)NULL, (LPARAM)HKCU_STR, 0, 100, 0); } @@ -557,13 +557,12 @@ __declspec(dllexport) void Update(HWND hwndParent, int string_size, TCHAR *varia pushstring(UpdateVar()); } -/* Our DLL entry point, this is called when we first load up our DLL. */ +/* Our DLL entry point, this is called when we first load up our DLL. + No CleanUp() on DLL_PROCESS_DETACH: NSISPluginCallback() already ran it at NSPIM_UNLOAD, a second run + freed the buffers twice and corrupted the installer's heap. */ BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInst, DWORD ul_reason_for_call, LPVOID lpReserved) { hInstance = hInst; - if (ul_reason_for_call == DLL_PROCESS_DETACH) - CleanUp(); - return TRUE; }