2
0
Эх сурвалжийг харах

Fix memory leak when `_ensure_cache_for_size()` fails

Haoyu Qiu 2 жил өмнө
parent
commit
6db8e79eed

+ 6 - 1
modules/text_server_adv/text_server_adv.cpp

@@ -1393,7 +1393,10 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
 		int error = 0;
 		int error = 0;
 		if (!ft_library) {
 		if (!ft_library) {
 			error = FT_Init_FreeType(&ft_library);
 			error = FT_Init_FreeType(&ft_library);
-			ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
+			if (error != 0) {
+				memdelete(fd);
+				ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
+			}
 		}
 		}
 
 
 		memset(&fd->stream, 0, sizeof(FT_StreamRec));
 		memset(&fd->stream, 0, sizeof(FT_StreamRec));
@@ -1422,6 +1425,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
 		if (error) {
 		if (error) {
 			FT_Done_Face(fd->face);
 			FT_Done_Face(fd->face);
 			fd->face = nullptr;
 			fd->face = nullptr;
+			memdelete(fd);
 			ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
 			ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
 		}
 		}
 
 
@@ -1835,6 +1839,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_f
 			FT_Done_MM_Var(ft_library, amaster);
 			FT_Done_MM_Var(ft_library, amaster);
 		}
 		}
 #else
 #else
+		memdelete(fd);
 		ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
 		ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
 #endif
 #endif
 	} else {
 	} else {

+ 6 - 1
modules/text_server_fb/text_server_fb.cpp

@@ -818,7 +818,10 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
 		int error = 0;
 		int error = 0;
 		if (!ft_library) {
 		if (!ft_library) {
 			error = FT_Init_FreeType(&ft_library);
 			error = FT_Init_FreeType(&ft_library);
-			ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
+			if (error != 0) {
+				memdelete(fd);
+				ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
+			}
 		}
 		}
 
 
 		memset(&fd->stream, 0, sizeof(FT_StreamRec));
 		memset(&fd->stream, 0, sizeof(FT_StreamRec));
@@ -847,6 +850,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
 		if (error) {
 		if (error) {
 			FT_Done_Face(fd->face);
 			FT_Done_Face(fd->face);
 			fd->face = nullptr;
 			fd->face = nullptr;
+			memdelete(fd);
 			ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
 			ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
 		}
 		}
 
 
@@ -945,6 +949,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_f
 			FT_Done_MM_Var(ft_library, amaster);
 			FT_Done_MM_Var(ft_library, amaster);
 		}
 		}
 #else
 #else
+		memdelete(fd);
 		ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
 		ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
 #endif
 #endif
 	}
 	}