Cocoa: Fix crash when closing window from title bar

Some mouse event may result in the window being deleted
so we need to take extra precaution when calling the super
class' 'sendEvent:' method.

Task-number: QTBUG-37287
Change-Id: Idf89ea177c78053bcdef52c54a197409e20bf38e
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
This commit is contained in:
Gabriel de Dietrich 2014-03-11 14:18:56 +01:00 committed by The Qt Project
parent 8bbc1eaeb6
commit 48e6352d4c
2 changed files with 15 additions and 0 deletions

View File

@ -80,6 +80,7 @@ typedef NSWindow<QNSWindowProtocol> QCocoaNSWindow;
- (id)initWithNSWindow:(QCocoaNSWindow *)window platformWindow:(QCocoaWindow *)platformWindow;
- (void)handleWindowEvent:(NSEvent *)theEvent;
- (void) clearWindow;
@end

View File

@ -155,7 +155,14 @@ static bool isMouseEvent(NSEvent *ev)
}
}
// The call to -[NSWindow sendEvent] may result in the window being deleted
// (e.g., when closing the window by pressing the title bar close button).
[self retain];
[self.window superSendEvent:theEvent];
bool windowStillAlive = self.window != nil; // We need to read before releasing
[self release];
if (!windowStillAlive)
return;
if (!self.window.delegate)
return; // Already detached, pending NSAppKitDefined event
@ -179,6 +186,11 @@ static bool isMouseEvent(NSEvent *ev)
self.window.delegate = nil;
}
- (void)clearWindow
{
_window = nil;
}
- (void)dealloc
{
_window = nil;
@ -259,6 +271,7 @@ static bool isMouseEvent(NSEvent *ev)
- (void)dealloc
{
[_helper clearWindow];
[_helper release];
_helper = nil;
[super dealloc];
@ -319,6 +332,7 @@ static bool isMouseEvent(NSEvent *ev)
- (void)dealloc
{
[_helper clearWindow];
[_helper release];
_helper = nil;
[super dealloc];