Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1952847

Browse files
committed
Simplify and fix a memory leak.
svn path=/trunk/matplotlib/; revision=3778
1 parent deaa7a0 commit 1952847

2 files changed

Lines changed: 25 additions & 85 deletions

File tree

src/ft2font.cpp

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242

4343
FT_Library _ft2Library;
4444

45-
FT2Image::FT2Image() :
46-
_isDirty(true),
47-
_buffer(NULL),
48-
_width(0), _height(0),
49-
_rgbCopy(NULL),
50-
_rgbaCopy(NULL) {
51-
_VERBOSE("FT2Image::FT2Image");
52-
}
45+
// FT2Image::FT2Image() :
46+
// _isDirty(true),
47+
// _buffer(NULL),
48+
// _width(0), _height(0),
49+
// _rgbCopy(NULL),
50+
// _rgbaCopy(NULL) {
51+
// _VERBOSE("FT2Image::FT2Image");
52+
// }
5353

5454
FT2Image::FT2Image(unsigned long width, unsigned long height) :
5555
_isDirty(true),
@@ -65,75 +65,28 @@ FT2Image::~FT2Image() {
6565
_VERBOSE("FT2Image::~FT2Image");
6666
delete [] _buffer;
6767
_buffer=NULL;
68+
delete _rgbCopy;
69+
delete _rgbaCopy;
6870
}
6971

7072
void FT2Image::resize(unsigned long width, unsigned long height) {
7173
size_t numBytes = width*height;
7274

73-
if (_width != width || _height != height) {
75+
if (width != _width || height != _height) {
76+
if (numBytes > _width*_height) {
77+
delete [] _buffer;
78+
_buffer = new unsigned char [numBytes];
79+
}
80+
7481
_width = width;
7582
_height = height;
76-
77-
delete [] _buffer;
78-
_buffer = new unsigned char [numBytes];
7983
}
8084

81-
for (size_t n=0; n<numBytes; n++)
82-
_buffer[n] = 0;
85+
memset(_buffer, 0, numBytes);
8386

8487
_isDirty = true;
8588
}
8689

87-
char FT2Image::resize__doc__[] =
88-
"resize(width, height)\n"
89-
"\n"
90-
"Resize the dimensions of the image (it is cleared in the process).\n"
91-
;
92-
Py::Object
93-
FT2Image::py_resize(const Py::Tuple & args) {
94-
_VERBOSE("FT2Image::resize");
95-
96-
args.verify_length(2);
97-
98-
long x0 = Py::Int(args[0]);
99-
long y0 = Py::Int(args[1]);
100-
101-
resize(x0, y0);
102-
103-
return Py::Object();
104-
}
105-
106-
void FT2Image::clear() {
107-
_VERBOSE("FT2Image::clear");
108-
109-
_width = 0;
110-
_height = 0;
111-
_isDirty = true;
112-
delete [] _buffer;
113-
_buffer = NULL;
114-
if (_rgbCopy) {
115-
delete _rgbCopy;
116-
_rgbCopy = NULL;
117-
}
118-
if (_rgbaCopy) {
119-
delete _rgbaCopy;
120-
_rgbaCopy = NULL;
121-
}
122-
}
123-
char FT2Image::clear__doc__[] =
124-
"clear()\n"
125-
"\n"
126-
"Clear the contents of the image.\n"
127-
;
128-
Py::Object
129-
FT2Image::py_clear(const Py::Tuple & args) {
130-
args.verify_length(0);
131-
132-
clear();
133-
134-
return Py::Object();
135-
}
136-
13790
void
13891
FT2Image::draw_bitmap( FT_Bitmap* bitmap,
13992
FT_Int x,
@@ -345,9 +298,7 @@ void FT2Image::makeRgbaCopy() {
345298
unsigned char *dst = _rgbaCopy->_buffer;
346299

347300
while (src != src_end) {
348-
*dst++ = 0;
349-
*dst++ = 0;
350-
*dst++ = 0;
301+
dst += 3;
351302
*dst++ = *src++;
352303
}
353304
}
@@ -824,8 +775,7 @@ FT2Font::clear(const Py::Tuple & args) {
824775
_VERBOSE("FT2Font::clear");
825776
args.verify_length(0);
826777

827-
if (image)
828-
image->clear();
778+
delete image;
829779

830780
angle = 0.0;
831781

@@ -1194,11 +1144,9 @@ FT2Font::draw_glyphs_to_bitmap(const Py::Tuple & args) {
11941144
size_t width = (string_bbox.xMax-string_bbox.xMin) / 64 + 2;
11951145
size_t height = (string_bbox.yMax-string_bbox.yMin) / 64 + 2;
11961146

1197-
if (!image) {
1198-
image = new FT2Image(width, height);
1199-
} else {
1200-
image->resize(width, height);
1201-
}
1147+
Py_XDECREF(image);
1148+
image = NULL;
1149+
image = new FT2Image(width, height);
12021150

12031151
for ( size_t n = 0; n < glyphs.size(); n++ )
12041152
{
@@ -1764,10 +1712,6 @@ FT2Image::init_type() {
17641712
behaviors().name("FT2Image");
17651713
behaviors().doc("FT2Image");
17661714

1767-
add_varargs_method("clear", &FT2Image::py_clear,
1768-
FT2Image::clear__doc__);
1769-
add_varargs_method("resize", &FT2Image::py_resize,
1770-
FT2Image::resize__doc__);
17711715
add_varargs_method("write_bitmap", &FT2Image::py_write_bitmap,
17721716
FT2Image::write_bitmap__doc__);
17731717
add_varargs_method("draw_rect", &FT2Image::py_draw_rect,

src/ft2font.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ extern "C" {
2222
// the freetype string rendered into a width, height buffer
2323
class FT2Image : public Py::PythonExtension<FT2Image> {
2424
public:
25-
FT2Image();
25+
// FT2Image();
2626
FT2Image(unsigned long width, unsigned long height);
2727
~FT2Image();
2828

2929
static void init_type();
3030

31-
void resize(unsigned long width, unsigned long height);
32-
void clear();
3331
void draw_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y);
3432
void write_bitmap(const char* filename) const;
3533
void draw_rect(unsigned long x0, unsigned long y0,
@@ -41,10 +39,6 @@ class FT2Image : public Py::PythonExtension<FT2Image> {
4139
unsigned int get_height() const { return _height; };
4240
const unsigned char *const get_buffer() const { return _buffer; };
4341

44-
static char clear__doc__ [];
45-
Py::Object py_clear(const Py::Tuple & args);
46-
static char resize__doc__ [];
47-
Py::Object py_resize(const Py::Tuple & args);
4842
static char write_bitmap__doc__ [];
4943
Py::Object py_write_bitmap(const Py::Tuple & args);
5044
static char draw_rect__doc__ [];
@@ -71,6 +65,8 @@ class FT2Image : public Py::PythonExtension<FT2Image> {
7165

7266
void makeRgbCopy();
7367
void makeRgbaCopy();
68+
69+
void resize(unsigned long width, unsigned long height);
7470
};
7571

7672

0 commit comments

Comments
 (0)