Don't destroy the native window in wxNativeWindow itself by default

Leave ownership of the native window to the user code as it may want to reuse
it for some other purpose and provide an explicit Disown() function that can
be called if the user really wants wxWidgets to take ownership of the native
window.

In particular, this avoids problems when using ARC under OS X which resulted
in a double "release" before.
This commit is contained in:
Vadim Zeitlin
2015-11-10 16:09:21 +01:00
parent 359f4e21be
commit cb26668204
6 changed files with 120 additions and 10 deletions

View File

@@ -95,6 +95,16 @@ public:
(void)Create(parent, wxID_ANY, hwnd);
}
virtual ~NativeWindow()
{
// If you don't call this, you need to call DestroyWindow() later.
//
// Also notice that a HWND can't continue to exist under MSW if its
// parent its destroyed, so you may also want to reparent it under some
// other window if the parent of this window is also getting destroyed.
Disown();
}
protected:
// This code requires NMBCDROPDOWN to work, we don't really want to
// reproduce its definition here for very old compilers not having it.
@@ -146,9 +156,22 @@ public:
);
#endif // GTK+ 3.6/earlier
g_object_ref_sink(widget);
(void)Create(parent, wxID_ANY, widget);
}
virtual ~NativeWindow()
{
// If you don't call this, you need to call g_object_unref() on the
// widget yourself. The advantage of this is that you don't necessarily
// have to do it right now and could keep using the native widget and
// destroy it later. But if you don't need it any longer, as is the
// case here, it's simpler to just call Disown() to let it be destroyed
// immediately.
Disown();
}
private:
wxMenu m_menu;
};
@@ -185,6 +208,15 @@ public:
(void)Create(parent, wxID_ANY, v);
}
virtual ~NativeWindow()
{
// If you don't call this, you need to call -release: on the button
// manually (see the comment in wxGTK version above) if using manual
// reference counting. If you build with ARC, you must *not* call
// Disown() to let the native view be destroyed automatically.
Disown();
}
};
#else // some other platform