Selaa lähdekoodia

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.
Marc-Antoine Lortie 5 vuotta sitten
vanhempi
commit
4d27fccd0b

+ 42 - 22
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<int>(msg.wParam);
 }