From 955054afeebd84f6a78ef89e3da12275d43ca23e Mon Sep 17 00:00:00 2001 From: Greg Lucas Date: Thu, 25 Nov 2021 16:11:51 -0700 Subject: [PATCH] MNT: Clean up some UTF strings and memory autorelease Use UTF-8 strings instead of C strings with encodings. Use autorelease semantics instead of explicitly releasing. --- src/_macosx.m | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/_macosx.m b/src/_macosx.m index 366b786cf60a..06a9c55cc244 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -303,7 +303,7 @@ static CGFloat _get_device_scale(CGContextRef cr) lazy_init(); FigureCanvas *self = (FigureCanvas*)type->tp_alloc(type, 0); if (!self) { return NULL; } - self->view = [View alloc]; + self->view = [[View alloc] autorelease]; return (PyObject*)self; } @@ -337,7 +337,6 @@ static CGFloat _get_device_scale(CGContextRef cr) { if (self->view) { [self->view setCanvas: NULL]; - [self->view release]; } Py_TYPE(self)->tp_free((PyObject*)self); } @@ -603,11 +602,10 @@ static CGFloat _get_device_scale(CGContextRef cr) FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { lazy_init(); - Window* window = [Window alloc]; + Window* window = [[Window alloc] autorelease]; if (!window) { return NULL; } FigureManager *self = (FigureManager*)type->tp_alloc(type, 0); if (!self) { - [window release]; return NULL; } self->window = window; @@ -719,9 +717,7 @@ static CGFloat _get_device_scale(CGContextRef cr) } Window* window = self->window; if (window) { - NSString* ns_title = [[[NSString alloc] - initWithCString: title - encoding: NSUTF8StringEncoding] autorelease]; + NSString* ns_title = [NSString stringWithUTF8String: title]; [window setTitle: ns_title]; } Py_RETURN_NONE; @@ -981,11 +977,10 @@ -(void)save_figure:(id)sender NavigationToolbar2_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { lazy_init(); - NavigationToolbar2Handler* handler = [NavigationToolbar2Handler alloc]; + NavigationToolbar2Handler* handler = [[NavigationToolbar2Handler alloc] autorelease]; if (!handler) { return NULL; } NavigationToolbar2 *self = (NavigationToolbar2*)type->tp_alloc(type, 0); if (!self) { - [handler release]; return NULL; } self->handler = handler; @@ -1062,12 +1057,10 @@ -(void)save_figure:(id)sender rect.origin.y = 0.5*(height - rect.size.height); for (int i = 0; i < 7; i++) { - NSString* filename = [NSString stringWithCString: images[i] - encoding: NSUTF8StringEncoding]; - NSString* tooltip = [NSString stringWithCString: tooltips[i] - encoding: NSUTF8StringEncoding]; - NSImage* image = [[NSImage alloc] initWithContentsOfFile: filename]; - buttons[i] = [[NSButton alloc] initWithFrame: rect]; + NSString* filename = [NSString stringWithUTF8String: images[i]]; + NSString* tooltip = [NSString stringWithUTF8String: tooltips[i]]; + NSImage* image = [[[NSImage alloc] initWithContentsOfFile: filename] autorelease]; + buttons[i] = [[[NSButton alloc] initWithFrame: rect] autorelease]; [image setSize: size]; [buttons[i] setBezelStyle: NSBezelStyleShadowlessSquare]; [buttons[i] setButtonType: buttontypes[i]]; @@ -1076,8 +1069,6 @@ -(void)save_figure:(id)sender [buttons[i] setImagePosition: NSImageOnly]; [buttons[i] setToolTip: tooltip]; [[window contentView] addSubview: buttons[i]]; - [buttons[i] release]; - [image release]; rect.origin.x += rect.size.width + gap; } @@ -1102,7 +1093,6 @@ -(void)save_figure:(id)sender rect.origin.y = 0.5 * (height - rect.size.height); [messagebox setFrameOrigin: rect.origin]; [[window contentView] addSubview: messagebox]; - [messagebox release]; [[window contentView] display]; self->messagebox = messagebox; @@ -1112,7 +1102,6 @@ -(void)save_figure:(id)sender static void NavigationToolbar2_dealloc(NavigationToolbar2 *self) { - [self->handler release]; Py_TYPE(self)->tp_free((PyObject*)self); } @@ -1215,15 +1204,10 @@ -(void)save_figure:(id)sender return NULL; } NSSavePanel* panel = [NSSavePanel savePanel]; - [panel setTitle: [NSString stringWithCString: title - encoding: NSASCIIStringEncoding]]; - NSString* ns_default_filename = - [[NSString alloc] - initWithCString: default_filename - encoding: NSUTF8StringEncoding]; + [panel setTitle: [NSString stringWithUTF8String: title]]; + NSString* ns_default_filename = [NSString stringWithUTF8String: default_filename]; [panel setNameFieldStringValue: ns_default_filename]; result = [panel runModal]; - [ns_default_filename release]; if (result == NSModalResponseOK) { NSURL* url = [panel URL]; NSString* filename = [url path];