From 4d27fccd0bec68a5d2525a740b1ab1eb34d0be1a Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lortie Date: Wed, 19 Feb 2020 13:17:45 -0500 Subject: [PATCH] Fixed memory leaks in SimpleTexturedOpenGL sample. - Added function "cleanup" to centralize release of resources. - Added function "freeTextureIds" to free textureIds memory. - Added call to freeTextureIds in LoadGLTextures to free memory before it is allocated again. - Made several changes in KillGLWindow to prevent certain Win32 handle/resources from being released more than once. --- .../src/model_loading.cpp | 64 ++++++++++++------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp b/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp index 8428248d1..8c2a08b2e 100644 --- a/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp +++ b/samples/SimpleTexturedOpenGL/SimpleTexturedOpenGL/src/model_loading.cpp @@ -202,8 +202,21 @@ std::string getBasePath(const std::string& path) return (std::string::npos == pos) ? "" : path.substr(0, pos + 1); } +void freeTextureIds() +{ + textureIdMap.clear(); //no need to delete pointers in it manually here. (Pointers point to textureIds deleted in next step) + + if (textureIds) + { + delete[] textureIds; + textureIds = NULL; + } +} + int LoadGLTextures(const aiScene* scene) { + freeTextureIds(); + //ILboolean success; /* Before calling ilInit() version should be checked. */ @@ -576,21 +589,24 @@ void KillGLWindow() // Properly Kill The Window hRC = NULL; } - if (hDC && !ReleaseDC(hWnd, hDC)) // Are We able to Release The DC? + if (hDC) { - MessageBox(NULL, TEXT("Release Device Context Failed."), TEXT("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION); - hDC=NULL; + if (!ReleaseDC(hWnd, hDC)) // Are We able to Release The DC? + MessageBox(NULL, TEXT("Release Device Context Failed."), TEXT("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION); + hDC = NULL; } - if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window + if (hWnd) { - MessageBox(NULL, TEXT("Could Not Release hWnd."), TEXT("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION); + if (!DestroyWindow(hWnd)) // Are We Able To Destroy The Window + MessageBox(NULL, TEXT("Could Not Release hWnd."), TEXT("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION); hWnd = NULL; - } + } - if (!UnregisterClass(TEXT("OpenGL"), hInstance)) // Are We Able To Unregister Class + if (hInstance) { - MessageBox(NULL, TEXT("Could Not Unregister Class."), TEXT("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION); + if (!UnregisterClass(TEXT("OpenGL"), hInstance)) // Are We Able To Unregister Class + MessageBox(NULL, TEXT("Could Not Unregister Class."), TEXT("SHUTDOWN ERROR"), MB_OK | MB_ICONINFORMATION); hInstance = NULL; } } @@ -761,6 +777,16 @@ BOOL CreateGLWindow(const char* title, int width, int height, int bits, bool ful return TRUE; } +void cleanup() +{ + freeTextureIds(); + + destroyAILogger(); + + if (hWnd) + KillGLWindow(); +}; + LRESULT CALLBACK WndProc(HWND hWnd, // Handles for this Window UINT uMsg, // Message for this Window WPARAM wParam, // additional message Info @@ -842,7 +868,11 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance modelpath = UTFConverter(modelpathW).str(); } - if (!Import3DFromFile(modelpath)) return 0; + if (!Import3DFromFile(modelpath)) + { + cleanup(); + return 0; + } logInfo("=============== Post Import ===================="); @@ -853,6 +883,7 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance if (!CreateGLWindow(windowTitle, 640, 480, 16, fullscreen)) { + cleanup(); return 0; } @@ -893,6 +924,7 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance fullscreen=!fullscreen; if (!CreateGLWindow(windowTitle, 640, 480, 16, fullscreen)) { + cleanup(); return 0; } } @@ -900,18 +932,6 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance } // *** cleanup *** - - textureIdMap.clear(); //no need to delete pointers in it manually here. (Pointers point to textureIds deleted in next step) - - if (textureIds) - { - delete[] textureIds; - textureIds = NULL; - } - - // *** cleanup end *** - - destroyAILogger(); - KillGLWindow(); + cleanup(); return static_cast(msg.wParam); }