Merge pull request #3021 from malortie/fix-simpletexturedopengl-memory-leaks

Fixed memory leaks in SimpleTexturedOpenGL sample.
pull/3014/head^2
Kim Kulling 2020-02-19 20:13:02 +01:00 committed by GitHub
commit bc3ed5f702
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 22 deletions

View File

@ -202,8 +202,21 @@ std::string getBasePath(const std::string& path)
return (std::string::npos == pos) ? "" : path.substr(0, pos + 1); 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) int LoadGLTextures(const aiScene* scene)
{ {
freeTextureIds();
//ILboolean success; //ILboolean success;
/* Before calling ilInit() version should be checked. */ /* Before calling ilInit() version should be checked. */
@ -576,21 +589,24 @@ void KillGLWindow() // Properly Kill The Window
hRC = NULL; 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); if (!ReleaseDC(hWnd, hDC)) // Are We able to Release The DC?
hDC=NULL; 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; 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; hInstance = NULL;
} }
} }
@ -761,6 +777,16 @@ BOOL CreateGLWindow(const char* title, int width, int height, int bits, bool ful
return TRUE; return TRUE;
} }
void cleanup()
{
freeTextureIds();
destroyAILogger();
if (hWnd)
KillGLWindow();
};
LRESULT CALLBACK WndProc(HWND hWnd, // Handles for this Window LRESULT CALLBACK WndProc(HWND hWnd, // Handles for this Window
UINT uMsg, // Message for this Window UINT uMsg, // Message for this Window
WPARAM wParam, // additional message Info WPARAM wParam, // additional message Info
@ -842,7 +868,11 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance
modelpath = UTFConverter(modelpathW).str(); modelpath = UTFConverter(modelpathW).str();
} }
if (!Import3DFromFile(modelpath)) return 0; if (!Import3DFromFile(modelpath))
{
cleanup();
return 0;
}
logInfo("=============== Post Import ===================="); logInfo("=============== Post Import ====================");
@ -853,6 +883,7 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance
if (!CreateGLWindow(windowTitle, 640, 480, 16, fullscreen)) if (!CreateGLWindow(windowTitle, 640, 480, 16, fullscreen))
{ {
cleanup();
return 0; return 0;
} }
@ -893,6 +924,7 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance
fullscreen=!fullscreen; fullscreen=!fullscreen;
if (!CreateGLWindow(windowTitle, 640, 480, 16, fullscreen)) if (!CreateGLWindow(windowTitle, 640, 480, 16, fullscreen))
{ {
cleanup();
return 0; return 0;
} }
} }
@ -900,18 +932,6 @@ int WINAPI WinMain( HINSTANCE hInstance, // The instance
} }
// *** cleanup *** // *** cleanup ***
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();
return static_cast<int>(msg.wParam); return static_cast<int>(msg.wParam);
} }