Use wxScopedPtr<> instead of explicit calls to "delete"

No real changes, but the code is a bit safer and hopefully more clear.
This commit is contained in:
Vadim Zeitlin
2020-08-14 19:41:53 +02:00
parent c70a8261cb
commit cdd68da370
3 changed files with 25 additions and 28 deletions

View File

@@ -59,10 +59,14 @@ public:
void Destroy(); void Destroy();
protected: protected:
// Note: only one of the following functions should be overridden, if both
// of them are, GetPopupMenu() has the priority, i.e. CreatePopupMenu()
// won't be called if GetPopupMenu() returns a non-null pointer.
// creates menu to be displayed when user clicks on the icon // creates menu to be displayed when user clicks on the icon
virtual wxMenu *CreatePopupMenu() { return NULL; } virtual wxMenu *CreatePopupMenu() { return NULL; }
// same as CreatePopupMenu but won't destroy the menu // same as CreatePopupMenu but the returned menu won't be destroyed
virtual wxMenu *GetPopupMenu() { return NULL; } virtual wxMenu *GetPopupMenu() { return NULL; }
private: private:

View File

@@ -25,6 +25,8 @@
#include "wx/menu.h" #include "wx/menu.h"
#endif #endif
#include "wx/scopedptr.h"
extern WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete; extern WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete;
// DLL options compatibility check: // DLL options compatibility check:
@@ -47,19 +49,18 @@ wxEND_EVENT_TABLE()
void wxTaskBarIconBase::OnRightButtonDown(wxTaskBarIconEvent& WXUNUSED(event)) void wxTaskBarIconBase::OnRightButtonDown(wxTaskBarIconEvent& WXUNUSED(event))
{ {
wxMenu *menu = CreatePopupMenu(); wxScopedPtr<wxMenu> menuDeleter;
if (menu) wxMenu *menu = GetPopupMenu();
if ( !menu )
{ {
PopupMenu(menu); menu = CreatePopupMenu();
delete menu; if ( !menu )
} return;
else
{
menu = GetPopupMenu();
if (menu) menuDeleter.reset(menu);
PopupMenu(menu);
} }
PopupMenu(menu);
} }
void wxTaskBarIconBase::Destroy() void wxTaskBarIconBase::Destroy()

View File

@@ -19,6 +19,7 @@
#include "wx/dcclient.h" #include "wx/dcclient.h"
#endif #endif
#include "wx/scopedptr.h"
#include "wx/taskbar.h" #include "wx/taskbar.h"
#include "wx/osx/private.h" #include "wx/osx/private.h"
@@ -102,7 +103,7 @@ protected:
private: private:
wxTaskBarIconDockImpl(); wxTaskBarIconDockImpl();
wxMenu *m_pMenu; wxMenu *m_pMenu;
bool m_keepMenu; wxScopedPtr<wxMenu> m_menuDeleter;
}; };
class wxTaskBarIconCustomStatusItemImpl; class wxTaskBarIconCustomStatusItemImpl;
@@ -231,7 +232,6 @@ wxTaskBarIconDockImpl::wxTaskBarIconDockImpl(wxTaskBarIcon *taskBarIcon)
wxASSERT_MSG(!sm_dockIcon, wxT("You should never have more than one dock icon!")); wxASSERT_MSG(!sm_dockIcon, wxT("You should never have more than one dock icon!"));
sm_dockIcon = this; sm_dockIcon = this;
m_pMenu = NULL; m_pMenu = NULL;
m_keepMenu = false;
} }
wxTaskBarIconDockImpl::~wxTaskBarIconDockImpl() wxTaskBarIconDockImpl::~wxTaskBarIconDockImpl()
@@ -250,28 +250,21 @@ WX_NSMenu wxTaskBarIconDockImpl::OSXGetDockHMenu()
WX_NSMenu wxTaskBarIconDockImpl::OSXDoGetDockHMenu() WX_NSMenu wxTaskBarIconDockImpl::OSXDoGetDockHMenu()
{ {
wxMenu *dockMenu = CreatePopupMenu(); m_menuDeleter.reset();
bool keepMenu;
wxMenu *dockMenu = GetPopupMenu();
if(!dockMenu) if(!dockMenu)
{ {
dockMenu = GetPopupMenu(); dockMenu = CreatePopupMenu();
if(!dockMenu) if(!dockMenu)
return nil; return nil;
keepMenu = true;
}
else
{
keepMenu = false;
}
if(!m_keepMenu) m_menuDeleter.reset(dockMenu);
wxDELETE(m_pMenu); }
m_pMenu = dockMenu; m_pMenu = dockMenu;
m_keepMenu = keepMenu;
m_pMenu->SetInvokingWindow(m_eventWindow); m_pMenu->SetInvokingWindow(m_eventWindow);
@@ -289,8 +282,7 @@ bool wxTaskBarIconDockImpl::SetIcon(const wxIcon& icon, const wxString& WXUNUSED
bool wxTaskBarIconDockImpl::RemoveIcon() bool wxTaskBarIconDockImpl::RemoveIcon()
{ {
if(!m_keepMenu) m_menuDeleter.reset();
wxDELETE(m_pMenu);
m_icon = wxBitmap(); m_icon = wxBitmap();
[[NSApplication sharedApplication] setApplicationIconImage:nil]; [[NSApplication sharedApplication] setApplicationIconImage:nil];