Cocoa integration - add a protection against dangling pointers
Backport watcher-sentinel trick (QPointer->QObject) from dev. Task-number: QTBUG-42059 Change-Id: I9b2c7cde635c2ed9a3f667f216da62870d0b5ccb Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
parent
072485048f
commit
5b54c352ed
@ -66,6 +66,7 @@ typedef NSWindow<QNSWindowProtocol> QCocoaNSWindow;
|
||||
QCocoaWindow *_platformWindow;
|
||||
BOOL _grabbingMouse;
|
||||
BOOL _releaseOnMouseUp;
|
||||
QPointer<QObject> _watcher;
|
||||
}
|
||||
|
||||
@property (nonatomic, readonly) QCocoaNSWindow *window;
|
||||
@ -315,6 +316,11 @@ public: // for QNSView
|
||||
};
|
||||
QHash<quintptr, BorderRange> m_contentBorderAreas; // identifer -> uppper/lower
|
||||
QHash<quintptr, bool> m_enabledContentBorderAreas; // identifer -> enabled state (true/false)
|
||||
|
||||
// This object is tracked by a 'watcher'
|
||||
// object in a window helper, preventing use of dangling
|
||||
// pointers.
|
||||
QObject sentinel;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -94,6 +94,7 @@ static bool isMouseEvent(NSEvent *ev)
|
||||
// make sure that m_nsWindow stays valid until the
|
||||
// QCocoaWindow is deleted by Qt.
|
||||
[_window setReleasedWhenClosed:NO];
|
||||
_watcher = &_platformWindow->sentinel;
|
||||
}
|
||||
|
||||
return self;
|
||||
@ -102,7 +103,7 @@ static bool isMouseEvent(NSEvent *ev)
|
||||
- (void)handleWindowEvent:(NSEvent *)theEvent
|
||||
{
|
||||
QCocoaWindow *pw = self.platformWindow;
|
||||
if (pw && pw->m_forwardWindow) {
|
||||
if (_watcher && pw && pw->m_forwardWindow) {
|
||||
if (theEvent.type == NSLeftMouseUp || theEvent.type == NSLeftMouseDragged) {
|
||||
QNSView *forwardView = pw->m_qtView;
|
||||
if (theEvent.type == NSLeftMouseUp) {
|
||||
@ -141,7 +142,7 @@ static bool isMouseEvent(NSEvent *ev)
|
||||
if (!self.window.delegate)
|
||||
return; // Already detached, pending NSAppKitDefined event
|
||||
|
||||
if (pw && pw->frameStrutEventsEnabled() && isMouseEvent(theEvent)) {
|
||||
if (_watcher && pw && pw->frameStrutEventsEnabled() && isMouseEvent(theEvent)) {
|
||||
NSPoint loc = [theEvent locationInWindow];
|
||||
NSRect windowFrame = [self.window convertRectFromScreen:[self.window frame]];
|
||||
NSRect contentFrame = [[self.window contentView] frame];
|
||||
@ -157,6 +158,7 @@ static bool isMouseEvent(NSEvent *ev)
|
||||
- (void)detachFromPlatformWindow
|
||||
{
|
||||
_platformWindow = 0;
|
||||
_watcher.clear();
|
||||
[self.window.delegate release];
|
||||
self.window.delegate = nil;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user