diff --git a/src/AudacityApp.cpp b/src/AudacityApp.cpp index 176196fc4..3f513961a 100644 --- a/src/AudacityApp.cpp +++ b/src/AudacityApp.cpp @@ -1766,33 +1766,34 @@ bool AudacityApp::CreateSingleInstanceChecker(const wxString &dir) wxUNIXaddress addr; addr.Filename(sockFile); - // Setup the socket - wxSocketClient *sock = new wxSocketClient(); - sock->SetFlags(wxSOCKET_WAITALL); - - // We try up to 50 times since there's a small window - // where the server may not have been fully initialized. - for (int i = 0; i < 50; i++) { - // Connect to the existing Audacity - sock->Connect(addr, true); - if (sock->IsConnected()) + // Setup the socket + // A wxSocketClient must not be deleted by us, but rather, let the + // framework do appropriate delayed deletion after Destroy() + Destroy_ptr sock { safenew wxSocketClient() }; + sock->SetFlags(wxSOCKET_WAITALL); + + // We try up to 50 times since there's a small window + // where the server may not have been fully initialized. + for (int i = 0; i < 50; i++) { - for (size_t i = 0, cnt = parser->GetParamCount(); i < cnt; i++) + // Connect to the existing Audacity + sock->Connect(addr, true); + if (sock->IsConnected()) { - // Send the filename - wxString param = parser->GetParam(i); - sock->WriteMsg((const wxChar *) param.c_str(), (param.Len() + 1) * sizeof(wxChar)); + for (size_t i = 0, cnt = parser->GetParamCount(); i < cnt; i++) + { + // Send the filename + wxString param = parser->GetParam(i); + sock->WriteMsg((const wxChar *) param.c_str(), (param.Len() + 1) * sizeof(wxChar)); + } + + return false; } - sock->Destroy(); - return false; + wxMilliSleep(100); } - - wxMilliSleep(100); } - - sock->Destroy(); #endif // There is another copy of Audacity running. Force quit. diff --git a/src/Screenshot.cpp b/src/Screenshot.cpp index e9f048fc5..728d4c55b 100644 --- a/src/Screenshot.cpp +++ b/src/Screenshot.cpp @@ -111,14 +111,15 @@ class ScreenFrame final : public wxFrame DECLARE_EVENT_TABLE() }; -static ScreenFrame *mFrame = NULL; +using ScreenFramePtr = Destroy_ptr; +ScreenFramePtr mFrame; //////////////////////////////////////////////////////////////////////////////// void OpenScreenshotTools() { if (!mFrame) { - mFrame = new ScreenFrame(NULL, -1); + mFrame = ScreenFramePtr{ safenew ScreenFrame(NULL, -1) }; } mFrame->Show(); mFrame->Raise(); @@ -126,10 +127,7 @@ void OpenScreenshotTools() void CloseScreenshotTools() { - if (mFrame) { - mFrame->Destroy(); - mFrame = NULL; - } + mFrame.reset(); } //////////////////////////////////////////////////////////////////////////////// @@ -485,7 +483,8 @@ bool ScreenFrame::ProcessEvent(wxEvent & e) void ScreenFrame::OnCloseWindow(wxCloseEvent & WXUNUSED(event)) { - mFrame = NULL; + if (this == mFrame.get()) + mFrame.release(); Destroy(); } diff --git a/src/effects/Effect.cpp b/src/effects/Effect.cpp index f34a0eac1..4e3cbae5e 100644 --- a/src/effects/Effect.cpp +++ b/src/effects/Effect.cpp @@ -779,15 +779,15 @@ void Effect::Preview() wxDialog *Effect::CreateUI(wxWindow *parent, EffectUIClientInterface *client) { - EffectUIHost *dlg = new EffectUIHost(parent, this, client); + Destroy_ptr dlg + { safenew EffectUIHost{ parent, this, client} }; if (dlg->Initialize()) { - return dlg; + // release() is safe because parent will own it + return dlg.release(); } - delete dlg; - return NULL; } diff --git a/src/toolbars/ToolManager.cpp b/src/toolbars/ToolManager.cpp index d7bc77099..27172e9cb 100644 --- a/src/toolbars/ToolManager.cpp +++ b/src/toolbars/ToolManager.cpp @@ -363,7 +363,7 @@ ToolManager::ToolManager( AudacityProject *parent, wxWindow *topDockParent ) mLeft = std::make_unique( 3, &pt[0] ); // Create the indicator frame - mIndicator = new wxFrame( NULL, + mIndicator = FramePtr{ safenew wxFrame( NULL, wxID_ANY, wxEmptyString, wxDefaultPosition, @@ -372,7 +372,8 @@ ToolManager::ToolManager( AudacityProject *parent, wxWindow *topDockParent ) wxFRAME_SHAPED | wxNO_BORDER | wxFRAME_NO_TASKBAR | - wxSTAY_ON_TOP ); + wxSTAY_ON_TOP ) + }; // Hook the creation event...only needed on GTK, but doesn't hurt for all mIndicator->Connect( wxEVT_CREATE, @@ -462,9 +463,6 @@ ToolManager::~ToolManager() wxPaintEventHandler( ToolManager::OnIndicatorPaint ), NULL, this ); - - // Must destroy the window since it doesn't have a parent - mIndicator->Destroy(); } // This table describes the default configuration of the toolbars as diff --git a/src/toolbars/ToolManager.h b/src/toolbars/ToolManager.h index 68aa5023f..0c1f69213 100644 --- a/src/toolbars/ToolManager.h +++ b/src/toolbars/ToolManager.h @@ -97,7 +97,8 @@ class ToolManager final : public wxEvtHandler wxPoint mLastPos; wxRect mBarPos; - wxFrame *mIndicator; + using FramePtr = Destroy_ptr; + FramePtr mIndicator; std::unique_ptr mLeft, mDown; wxRegion *mCurrent;