From 83e9e7de970f0f4cca9da77559000a3d7afda2b2 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 31 Mar 2016 01:13:29 -0400 Subject: [PATCH] Clone functions required by wxWidgets base classes can use safenew --- src/Screenshot.cpp | 10 ++++++---- src/commands/AppCommandEvent.cpp | 2 +- src/widgets/Grabber.h | 3 ++- src/widgets/Grid.cpp | 9 ++++++--- src/widgets/Grid.h | 6 +++--- src/widgets/valnum.h | 6 ++++-- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Screenshot.cpp b/src/Screenshot.cpp index 8a600fd97..a2d37ada8 100644 --- a/src/Screenshot.cpp +++ b/src/Screenshot.cpp @@ -9,6 +9,7 @@ *******************************************************************/ #include "Screenshot.h" +#include "MemoryX.h" #include "commands/ScreenshotCommand.h" #include "commands/CommandTargets.h" #include "commands/CommandDirectory.h" @@ -140,20 +141,20 @@ class ScreenFrameTimer final : public wxTimer wxEvent & event) { screenFrame = frame; - evt = event.Clone(); + evt.reset(event.Clone()); } void Notify() override { + // Process timer notification just once, then destroy self evt->SetEventObject(NULL); screenFrame->ProcessEvent(*evt); - delete evt; delete this; } private: ScreenFrame *screenFrame; - wxEvent *evt; + std::unique_ptr evt; }; //////////////////////////////////////////////////////////////////////////////// @@ -463,7 +464,8 @@ bool ScreenFrame::ProcessEvent(wxEvent & e) e.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED && id >= IdAllDelayedEvents && id <= IdLastDelayedEvent && e.GetEventObject() != NULL) { - ScreenFrameTimer *timer = new ScreenFrameTimer(this, e); + // safenew because it's a one-shot that deletes itself + ScreenFrameTimer *timer = safenew ScreenFrameTimer(this, e); timer->Start(5000, true); return true; } diff --git a/src/commands/AppCommandEvent.cpp b/src/commands/AppCommandEvent.cpp index dd53d5a60..3ac04ab31 100644 --- a/src/commands/AppCommandEvent.cpp +++ b/src/commands/AppCommandEvent.cpp @@ -44,7 +44,7 @@ AppCommandEvent::~AppCommandEvent() // Clone is required by wxwidgets; implemented via copy constructor wxEvent *AppCommandEvent::Clone() const { - return new AppCommandEvent(*this); + return safenew AppCommandEvent(*this); } /// Store a pointer to a command object diff --git a/src/widgets/Grabber.h b/src/widgets/Grabber.h index 516e22b8e..28e1e8f75 100644 --- a/src/widgets/Grabber.h +++ b/src/widgets/Grabber.h @@ -68,9 +68,10 @@ class GrabberEvent final : public wxCommandEvent mPos = pos; } + // Clone is required by wxwidgets; implemented via copy constructor wxEvent *Clone() const override { - return new GrabberEvent(*this); + return safenew GrabberEvent(*this); } protected: diff --git a/src/widgets/Grid.cpp b/src/widgets/Grid.cpp index bc3ba3d81..d4a0189f0 100644 --- a/src/widgets/Grid.cpp +++ b/src/widgets/Grid.cpp @@ -125,9 +125,10 @@ bool TimeEditor::IsAcceptedKey(wxKeyEvent &event) return false; } +// Clone is required by wxwidgets; implemented via copy constructor wxGridCellEditor *TimeEditor::Clone() const { - return new TimeEditor(mFormat, mRate); + return safenew TimeEditor(mFormat, mRate); } wxString TimeEditor::GetValue() const @@ -246,9 +247,10 @@ wxSize TimeRenderer::GetBestSize(wxGrid &grid, return sz; } +// Clone is required by wxwidgets; implemented via copy constructor wxGridCellRenderer *TimeRenderer::Clone() const { - return new TimeRenderer(); + return safenew TimeRenderer(); } ChoiceEditor::ChoiceEditor(size_t count, const wxString choices[]) @@ -272,9 +274,10 @@ ChoiceEditor::~ChoiceEditor() mHandler.DisconnectEvent(m_control); } +// Clone is required by wxwidgets; implemented via copy constructor wxGridCellEditor *ChoiceEditor::Clone() const { - return new ChoiceEditor(mChoices); + return safenew ChoiceEditor(mChoices); } void ChoiceEditor::Create(wxWindow* parent, wxWindowID id, wxEvtHandler* evtHandler) diff --git a/src/widgets/Grid.h b/src/widgets/Grid.h index 9a98b2f66..728963d3f 100644 --- a/src/widgets/Grid.h +++ b/src/widgets/Grid.h @@ -67,7 +67,7 @@ class TimeEditor final : public wxGridCellEditor void SetFormat(const wxString &format); void SetRate(double rate); - wxGridCellEditor *Clone() const; + wxGridCellEditor *Clone() const override; wxString GetValue() const; NumericTextCtrl *GetTimeCtrl() const { return (NumericTextCtrl *)m_control; } @@ -104,7 +104,7 @@ class TimeRenderer final : public wxGridCellRenderer int row, int col); - wxGridCellRenderer *Clone() const; + wxGridCellRenderer *Clone() const override; }; // ---------------------------------------------------------------------------- @@ -141,7 +141,7 @@ public: void Reset(); - wxGridCellEditor *Clone() const; + wxGridCellEditor *Clone() const override; void SetChoices(const wxArrayString &choices); wxString GetValue() const; diff --git a/src/widgets/valnum.h b/src/widgets/valnum.h index e8e0dab6e..bd0bbf35f 100644 --- a/src/widgets/valnum.h +++ b/src/widgets/valnum.h @@ -348,7 +348,8 @@ public: this->DoSetMax(std::numeric_limits::max()); } - wxObject *Clone() const override { return new IntegerValidator(*this); } + // Clone is required by wxwidgets; implemented via copy constructor + wxObject *Clone() const override { return safenew IntegerValidator(*this); } private: DECLARE_NO_ASSIGN_CLASS(IntegerValidator); @@ -457,9 +458,10 @@ public: this->SetPrecision(precision); } + // Clone is required by wxwidgets; implemented via copy constructor wxObject *Clone() const override { - return new FloatingPointValidator(*this); + return safenew FloatingPointValidator(*this); } private: