From 93bdd9dc78ec926e3fdaee64ab004d4ab775a4ba Mon Sep 17 00:00:00 2001 From: xtemp09 Date: Sat, 15 Apr 2023 01:42:19 +0700 Subject: [PATCH] Fix memory leak when loading a font file - FreeCAD allocated memory to load a font file, but did not deallocate. std::unique_ptr fixed it. - Removed unnecessary includes --- src/Mod/Part/App/FT2FC.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Mod/Part/App/FT2FC.cpp b/src/Mod/Part/App/FT2FC.cpp index 24e8e739a6..0a0538d3b2 100644 --- a/src/Mod/Part/App/FT2FC.cpp +++ b/src/Mod/Part/App/FT2FC.cpp @@ -32,8 +32,6 @@ #ifndef _PreComp_ # include # include -# include -# include # include # include # include @@ -144,8 +142,8 @@ PyObject* FT2FC(const Py_UNICODE *PyUString, int bytesNeeded = fontfile.tellg(); fontfile.clear(); fontfile.seekg (0, fontfile.beg); - char* buffer = new char[bytesNeeded]; - fontfile.read(buffer, bytesNeeded); + auto buffer = std::unique_ptr (new char[bytesNeeded]); + fontfile.read(buffer.get(), bytesNeeded); if (!fontfile) { //get indignant ErrorMsg << "Can not read font file: " << FontSpec; @@ -153,7 +151,7 @@ PyObject* FT2FC(const Py_UNICODE *PyUString, } fontfile.close(); - const FT_Byte* ftBuffer = reinterpret_cast(buffer); + const FT_Byte* ftBuffer = reinterpret_cast(buffer.get()); error = FT_New_Memory_Face(FTLib, ftBuffer, bytesNeeded, FaceIndex, &FTFace); if (error) { ErrorMsg << "FT_New_Face failed: " << error;