Merge pull request #9267 from xtemp09/mem-fix

Fix memory leak when loading a font file
This commit is contained in:
Chris Hennes
2023-04-14 15:41:21 -05:00
committed by GitHub

View File

@@ -32,8 +32,6 @@
#ifndef _PreComp_
# include <iostream>
# include <fstream>
# include <string>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <stdexcept>
@@ -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 <char> (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<FT_Byte*>(buffer);
const FT_Byte* ftBuffer = reinterpret_cast<FT_Byte*>(buffer.get());
error = FT_New_Memory_Face(FTLib, ftBuffer, bytesNeeded, FaceIndex, &FTFace);
if (error) {
ErrorMsg << "FT_New_Face failed: " << error;