1- #if ! __has_feature(objc_arc)
2- #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
3- #endif
4-
51#define PY_SSIZE_T_CLEAN
62#include < Cocoa/Cocoa.h>
73#include < ApplicationServices/ApplicationServices.h>
@@ -374,14 +370,15 @@ static CGFloat _get_device_scale(CGContextRef cr)
374370FigureCanvas_dealloc (FigureCanvas* self)
375371{
376372 [self ->view setCanvas: NULL ];
373+ [self ->view release ];
377374 Py_TYPE (self)->tp_free ((PyObject*)self);
378375}
379376
380377static PyObject*
381378FigureCanvas_repr (FigureCanvas* self)
382379{
383380 return PyUnicode_FromFormat (" FigureCanvas object %p wrapping NSView %p " ,
384- (void *)self, (__bridge void *)(self->view ));
381+ (void *)self, (void *)(self->view ));
385382}
386383
387384static PyObject*
@@ -585,6 +582,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
585582 if (!window) { return NULL ; }
586583 FigureManager *self = (FigureManager*)type->tp_alloc (type, 0 );
587584 if (!self) {
585+ [window release ];
588586 return NULL ;
589587 }
590588 self->window = window;
@@ -637,7 +635,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
637635FigureManager_repr (FigureManager* self)
638636{
639637 return PyUnicode_FromFormat (" FigureManager object %p wrapping NSWindow %p " ,
640- (void *) self, (__bridge void *)(self->window ));
638+ (void *) self, (void *)(self->window ));
641639}
642640
643641static void
@@ -687,7 +685,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
687685 PyErr_SetString (PyExc_RuntimeError, " Could not convert to NSString*" );
688686 return NULL ;
689687 }
690- NSImage * image = [[NSImage alloc ] initByReferencingFile: ns_icon_path];
688+ NSImage * image = [[[ NSImage alloc ] initByReferencingFile: ns_icon_path] autorelease ];
691689 if (!image) {
692690 PyErr_SetString (PyExc_RuntimeError, " Could not create NSImage*" );
693691 return NULL ;
@@ -802,7 +800,7 @@ @interface NavigationToolbar2Handler : NSObject
802800 NSButton * zoombutton;
803801}
804802- (NavigationToolbar2Handler*)initWithToolbar : (PyObject*)toolbar ;
805- - (void )installCallbacks : (SEL [7 ])actions forButtons : (NSButton *__strong [7 ])buttons ;
803+ - (void )installCallbacks : (SEL [7 ])actions forButtons : (NSButton *[7 ])buttons ;
806804- (void )home : (id )sender ;
807805- (void )back : (id )sender ;
808806- (void )forward : (id )sender ;
@@ -823,12 +821,12 @@ - (void)save_figure:(id)sender;
823821@implementation NavigationToolbar2Handler
824822- (NavigationToolbar2Handler*)initWithToolbar : (PyObject*)theToolbar
825823{
826- self = [self init ];
824+ [self init ];
827825 toolbar = theToolbar;
828826 return self;
829827}
830828
831- - (void )installCallbacks : (SEL [7 ])actions forButtons : (NSButton *__strong [7 ])buttons
829+ - (void )installCallbacks : (SEL [7 ])actions forButtons : (NSButton *[7 ])buttons
832830{
833831 int i;
834832 for (i = 0 ; i < 7 ; i++) {
@@ -869,6 +867,7 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
869867 if (!handler) { return NULL ; }
870868 NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc (type, 0 );
871869 if (!self) {
870+ [handler release ];
872871 return NULL ;
873872 }
874873 self->handler = handler;
@@ -958,6 +957,8 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
958957 [buttons[i] setImagePosition: NSImageOnly];
959958 [buttons[i] setToolTip: tooltip];
960959 [[window contentView ] addSubview: buttons[i]];
960+ [buttons[i] release ];
961+ [image release ];
961962 rect.origin .x += rect.size .width + gap;
962963 }
963964
@@ -970,7 +971,7 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
970971 // Make it a zero-width box if we don't have enough room
971972 rect.size .width = fmax (bounds.size .width - rect.origin .x , 0 );
972973 rect.origin .x = bounds.size .width - rect.size .width ;
973- NSTextView * messagebox = [[NSTextView alloc ] initWithFrame: rect];
974+ NSTextView * messagebox = [[[ NSTextView alloc ] initWithFrame: rect] autorelease ];
974975 messagebox.textContainer .maximumNumberOfLines = 2 ;
975976 messagebox.textContainer .lineBreakMode = NSLineBreakByTruncatingTail;
976977 messagebox.alignment = NSTextAlignmentRight;
@@ -980,6 +981,7 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
980981 /* if selectable, the messagebox can become first responder,
981982 * which is not supposed to happen */
982983 [[window contentView ] addSubview: messagebox];
984+ [messagebox release ];
983985 [[window contentView ] display ];
984986
985987 self->messagebox = messagebox;
@@ -989,6 +991,7 @@ -(void)save_figure:(id)sender { gil_call_method(toolbar, "save_figure"); }
989991static void
990992NavigationToolbar2_dealloc (NavigationToolbar2 *self)
991993{
994+ [self ->handler release ];
992995 Py_TYPE (self)->tp_free ((PyObject*)self);
993996}
994997
@@ -1089,6 +1092,36 @@ + (WindowServerConnectionManager *)sharedManager
10891092 return sharedWindowServerConnectionManager;
10901093}
10911094
1095+ + (id )allocWithZone : (NSZone *)zone
1096+ {
1097+ return [[self sharedManager ] retain ];
1098+ }
1099+
1100+ + (id )copyWithZone : (NSZone *)zone
1101+ {
1102+ return self;
1103+ }
1104+
1105+ + (id )retain
1106+ {
1107+ return self;
1108+ }
1109+
1110+ - (NSUInteger )retainCount
1111+ {
1112+ return NSUIntegerMax; // denotes an object that cannot be released
1113+ }
1114+
1115+ - (oneway void )release
1116+ {
1117+ // Don't release a singleton object
1118+ }
1119+
1120+ - (id )autorelease
1121+ {
1122+ return self;
1123+ }
1124+
10921125- (void )launch : (NSNotification *)notification
10931126{
10941127 CFRunLoopRef runloop;
@@ -1166,6 +1199,7 @@ - (void)dealloc
11661199 * content view of this window was increased during the call to addSubview,
11671200 * and is decreased during the call to [super dealloc].
11681201 */
1202+ [super dealloc ];
11691203}
11701204@end
11711205
@@ -1187,6 +1221,7 @@ - (void)dealloc
11871221{
11881222 FigureCanvas* fc = (FigureCanvas*)canvas;
11891223 if (fc) { fc->view = NULL ; }
1224+ [super dealloc ];
11901225}
11911226
11921227- (void )setCanvas : (PyObject*)newCanvas
0 commit comments