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();
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
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; }
private:

View File

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

View File

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