Skip to content

Fix macosx segfault #17084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 5 additions & 34 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ static int wait_for_stdin(void)
}

NSEvent* event;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while (true) {
while (true) {
event = [NSApp nextEventMatchingMask: NSAnyEventMask
Expand All @@ -151,7 +150,6 @@ static int wait_for_stdin(void)
CFRunLoopRun();
if (interrupted || CFReadStreamHasBytesAvailable(stream)) break;
}
[pool release];

if (py_sigint_handler) PyOS_setsig(SIGINT, py_sigint_handler);
CFReadStreamUnscheduleFromRunLoop(stream,
Expand Down Expand Up @@ -280,15 +278,13 @@ static void lazy_init(void) {
PyOS_InputHook = wait_for_stdin;
#endif

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
WindowServerConnectionManager* connectionManager = [WindowServerConnectionManager sharedManager];
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSNotificationCenter* notificationCenter = [workspace notificationCenter];
[notificationCenter addObserver: connectionManager
selector: @selector(launch:)
name: NSWorkspaceDidLaunchApplicationNotification
object: nil];
[pool release];
}

static PyObject*
Expand Down Expand Up @@ -366,11 +362,7 @@ static CGFloat _get_device_scale(CGContextRef cr)

if(view) /* The figure may have been closed already */
{
/* Whereas drawRect creates its own autorelease pool, apparently
* [view display] also needs one. Create and release it here. */
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[view display];
[pool release];
}

Py_RETURN_NONE;
Expand Down Expand Up @@ -506,7 +498,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
close(channel[0]);
}

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSDate* date =
(timeout > 0.0) ? [NSDate dateWithTimeIntervalSinceNow: timeout]
: [NSDate distantFuture];
Expand All @@ -518,7 +509,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
if (!event || [event type]==NSApplicationDefined) break;
[NSApp sendEvent: event];
}
[pool release];

if (py_sigint_handler) PyOS_setsig(SIGINT, py_sigint_handler);

Expand Down Expand Up @@ -691,7 +681,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
rect.size.height = height;
rect.size.width = width;

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
self->window = [self->window initWithContentRect: rect
styleMask: NSTitledWindowMask
| NSClosableWindowMask
Expand All @@ -709,7 +698,6 @@ static CGFloat _get_device_scale(CGContextRef cr)
[window makeFirstResponder: view];
[[window contentView] addSubview: view];

[pool release];
return 0;
}

Expand All @@ -726,9 +714,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[window close];
[pool release];
}
Py_TYPE(self)->tp_free((PyObject*)self);
}
Expand All @@ -739,10 +725,8 @@ static CGFloat _get_device_scale(CGContextRef cr)
Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[window makeKeyAndOrderFront: nil];
[window orderFrontRegardless];
[pool release];
}
Py_RETURN_NONE;
}
Expand All @@ -753,9 +737,7 @@ static CGFloat _get_device_scale(CGContextRef cr)
Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[window close];
[pool release];
self->window = NULL;
}
Py_RETURN_NONE;
Expand All @@ -772,12 +754,10 @@ static CGFloat _get_device_scale(CGContextRef cr)
Window* window = self->window;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* ns_title = [[[NSString alloc]
initWithCString: title
encoding: NSUTF8StringEncoding] autorelease];
[window setTitle: ns_title];
[pool release];
}
Py_RETURN_NONE;
}
Expand All @@ -789,13 +769,11 @@ static CGFloat _get_device_scale(CGContextRef cr)
PyObject* result = NULL;
if(window)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* title = [window title];
if (title) {
const char* cTitle = [title UTF8String];
result = PyUnicode_FromString(cTitle);
}
[pool release];
}
if (result) {
return result;
Expand Down Expand Up @@ -1100,9 +1078,9 @@ -(void)save_figure:(id)sender
const float gap = 2;
const int height = 36;
const int imagesize = 24;

self->height = height;

const char* basedir;

obj = PyObject_GetAttrString((PyObject*)self, "canvas");
Expand All @@ -1127,7 +1105,6 @@ -(void)save_figure:(id)sender

if(!PyArg_ParseTuple(args, "s", &basedir)) return -1;

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSRect bounds = [view bounds];
NSWindow* window = [view window];

Expand Down Expand Up @@ -1230,8 +1207,6 @@ -(void)save_figure:(id)sender
[messagebox release];
[[window contentView] display];

[pool release];

self->messagebox = messagebox;
return 0;
}
Expand Down Expand Up @@ -1262,24 +1237,22 @@ -(void)save_figure:(id)sender
NSTextView* messagebox = self->messagebox;

if (messagebox)
{ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
{
NSString* text = [NSString stringWithUTF8String: message];
[messagebox setString: text];

// Adjust width with the window size
NSRect rectWindow = [messagebox.superview frame];
NSRect rect = [messagebox frame];
rect.size.width = rectWindow.size.width - rect.origin.x;
[messagebox setFrame: rect];

// Adjust height with the content size
[messagebox.layoutManager ensureLayoutForTextContainer: messagebox.textContainer];
NSRect contentSize = [messagebox.layoutManager usedRectForTextContainer: messagebox.textContainer];
rect = [messagebox frame];
rect.origin.y = 0.5 * (self->height - contentSize.size.height);
[messagebox setFrame: rect];

[pool release];
}

Py_RETURN_NONE;
Expand Down Expand Up @@ -2317,14 +2290,12 @@ - (int)index
show(PyObject* self)
{
[NSApp activateIgnoringOtherApps: YES];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSArray *windowsArray = [NSApp windows];
NSEnumerator *enumerator = [windowsArray objectEnumerator];
NSWindow *window;
while ((window = [enumerator nextObject])) {
[window orderFront:nil];
}
[pool release];
Py_BEGIN_ALLOW_THREADS
[NSApp run];
Py_END_ALLOW_THREADS
Expand Down