Skip to content

Commit 21fe205

Browse files
kekekeksMrJul
authored andcommitted
Replace raw pointer with ComObjectWeakPtr<T> (#18757)
1 parent 55504db commit 21fe205

File tree

5 files changed

+11
-7
lines changed

5 files changed

+11
-7
lines changed

native/Avalonia.Native/src/OSX/AvnView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ - (void)mouseEvent:(NSEvent *)event withType:(AvnRawMouseEventType) type
363363
auto windowBase = _parent.tryGetWithCast<WindowBaseImpl>();
364364

365365
if(windowBase != nullptr){
366-
auto parent = windowBase->Parent;
366+
auto parent = windowBase->Parent.tryGet();
367367

368368
if(parent != nullptr){
369369
auto parentWindow = parent->Window;

native/Avalonia.Native/src/OSX/PopupImpl.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ virtual HRESULT Show(bool activate, bool isDialog) override
4545

4646
virtual bool ShouldTakeFocusOnShow() override
4747
{
48+
auto parent = Parent.tryGet();
4849
// Don't steal the focus from another windows if our parent is inactive
49-
if (Parent != nullptr && Parent->Window != nullptr && ![Parent->Window isKeyWindow])
50+
if (parent != nullptr && parent->Window != nullptr && ![parent->Window isKeyWindow])
5051
return false;
5152

5253
return WindowBaseImpl::ShouldTakeFocusOnShow();

native/Avalonia.Native/src/OSX/WindowBaseImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class WindowBaseImpl : public virtual TopLevelImpl,
108108
std::list<WindowBaseImpl*> _children;
109109

110110
public:
111-
WindowBaseImpl* Parent = nullptr;
111+
ComObjectWeakPtr<WindowBaseImpl> Parent = nullptr;
112112
NSWindow * Window;
113113
ComPtr<IAvnWindowBaseEvents> BaseEvents;
114114
};

native/Avalonia.Native/src/OSX/WindowBaseImpl.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,16 +503,19 @@
503503
START_COM_CALL;
504504

505505
@autoreleasepool {
506-
if(Parent != nullptr)
506+
507+
auto oldParent = Parent.tryGet();
508+
509+
if(oldParent != nullptr)
507510
{
508-
Parent->_children.remove(this);
511+
oldParent->_children.remove(this);
509512
}
510513

511514
auto cparent = dynamic_cast<WindowImpl *>(parent);
512515

513516
Parent = cparent;
514517

515-
if(Parent != nullptr && Window != nullptr){
518+
if(cparent != nullptr && Window != nullptr){
516519
// If one tries to show a child window with a minimized parent window, then the parent window will be
517520
// restored but macOS isn't kind enough to *tell* us that, so the window will be left in a non-interactive
518521
// state. Detect this and explicitly restore the parent window ourselves to avoid this situation.

native/Avalonia.Native/src/OSX/WindowImpl.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@
546546
}
547547

548548
bool WindowImpl::IsOwned() {
549-
return Parent != nullptr;
549+
return Parent.tryGet() != nullptr;
550550
}
551551

552552
NSWindowStyleMask WindowImpl::CalculateStyleMask() {

0 commit comments

Comments
 (0)