Skip to content

Commit 07c863d

Browse files
committed
Make sure that the FigureCanvas and the View are deallocated.
Previously, the Py_INCREF ensured that the reference count of FigureCanvas never reaches zero. The FigureCanvas was therefore never deallocated, and the View remained in memory. See bug #2889570 on bugzilla, reported by Nicholas Lederer. svn path=/trunk/matplotlib/; revision=7918
1 parent 2c5f4b9 commit 07c863d

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/_macosx.m

+23-12
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ @interface View : NSView
340340
- (void)dealloc;
341341
- (void)drawRect:(NSRect)rect;
342342
- (void)windowDidResize:(NSNotification*)notification;
343-
- (View*)initWithFrame:(NSRect)rect canvas:(PyObject*)fc;
343+
- (View*)initWithFrame:(NSRect)rect;
344+
- (void)setCanvas: (PyObject*)newCanvas;
344345
- (BOOL)windowShouldClose:(NSNotification*)notification;
345346
- (BOOL)isFlipped;
346347
- (void)mouseDown:(NSEvent*)event;
@@ -2896,10 +2897,22 @@ static void _data_provider_release(void* info, const void* data, size_t size)
28962897
if(!PyArg_ParseTuple(args, "ii", &width, &height)) return -1;
28972898

28982899
NSRect rect = NSMakeRect(0.0, 0.0, width, height);
2899-
self->view = [self->view initWithFrame: rect canvas: (PyObject*)self];
2900+
self->view = [self->view initWithFrame: rect];
2901+
[self->view setCanvas: (PyObject*)self];
29002902
return 0;
29012903
}
29022904

2905+
static void
2906+
FigureCanvas_dealloc(FigureCanvas* self)
2907+
{
2908+
if (self->view)
2909+
{
2910+
[self->view setCanvas: NULL];
2911+
[self->view release];
2912+
}
2913+
self->ob_type->tp_free((PyObject*)self);
2914+
}
2915+
29032916
static PyObject*
29042917
FigureCanvas_repr(FigureCanvas* self)
29052918
{
@@ -3243,7 +3256,7 @@ static void _data_provider_release(void* info, const void* data, size_t size)
32433256
"_macosx.FigureCanvas", /*tp_name*/
32443257
sizeof(FigureCanvas), /*tp_basicsize*/
32453258
0, /*tp_itemsize*/
3246-
0, /*tp_dealloc*/
3259+
(destructor)FigureCanvas_dealloc, /*tp_dealloc*/
32473260
0, /*tp_print*/
32483261
0, /*tp_getattr*/
32493262
0, /*tp_setattr*/
@@ -4483,27 +4496,25 @@ - (BOOL)isFlipped
44834496
return NO;
44844497
}
44854498

4486-
- (View*)initWithFrame:(NSRect)rect canvas: (PyObject*)fc
4499+
- (View*)initWithFrame:(NSRect)rect
44874500
{
44884501
self = [super initWithFrame: rect];
44894502
rubberband = NSZeroRect;
4490-
if (canvas)
4491-
{
4492-
Py_DECREF(canvas);
4493-
}
4494-
canvas = fc;
4495-
Py_INCREF(canvas);
44964503
return self;
44974504
}
44984505

44994506
- (void)dealloc
45004507
{
45014508
FigureCanvas* fc = (FigureCanvas*)canvas;
4502-
fc->view = NULL;
4503-
Py_DECREF(canvas);
4509+
if (fc) fc->view = NULL;
45044510
[super dealloc];
45054511
}
45064512

4513+
- (void)setCanvas: (PyObject*)newCanvas
4514+
{
4515+
canvas = newCanvas;
4516+
}
4517+
45074518
-(void)drawRect:(NSRect)rect
45084519
{
45094520
PyObject* result;

0 commit comments

Comments
 (0)