Calls to Disconnect or Unbind in destructors are not needed, if...

... it's either the source of the connection that is being destroyed, or other
object (such as an ancestor window) transitively owning it and so causing it to
be destroyed too;

or, the sink is being destroyed, and that sink is a wxEvtHandler (which is
always so for Disconnect, though not for Unbind in case Bind was passed a
member function of a non-wxEvtHandler).

wxWidgets takes care of erasing the connection in such cases.

This removes most calls to Disconnect and Unbind.  Many destructors shrank to
nothing.

Notably, in case of popup menu handling, the call to Disconnect is not removable
because the object being destroyed is neither the source nor the sink.
This commit is contained in:
Paul Licameli 2018-01-31 10:08:42 -05:00
parent 12983e1685
commit bf5228267a
29 changed files with 4 additions and 284 deletions

View File

@ -57,11 +57,6 @@ AudacityLogger::AudacityLogger()
mUpdated = false;
}
AudacityLogger::~AudacityLogger()
{
Destroy();
}
void AudacityLogger::Flush()
{
if (mUpdated && mFrame && mFrame->IsShown()) {
@ -95,50 +90,6 @@ void AudacityLogger::DoLogText(const wxString & str)
}
}
void AudacityLogger::Destroy()
{
if (mFrame) {
mFrame->Disconnect(LoggerID_Save,
wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(AudacityLogger::OnSave),
NULL,
this);
mFrame->Disconnect(LoggerID_Clear,
wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(AudacityLogger::OnClear),
NULL,
this);
mFrame->Disconnect(LoggerID_Close,
wxEVT_COMMAND_BUTTON_CLICKED,
wxCommandEventHandler(AudacityLogger::OnClose),
NULL,
this);
mFrame->Disconnect(LoggerID_Save,
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(AudacityLogger::OnSave),
NULL,
this);
mFrame->Disconnect(LoggerID_Clear,
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(AudacityLogger::OnClear),
NULL,
this);
mFrame->Disconnect(LoggerID_Close,
wxEVT_COMMAND_MENU_SELECTED,
wxCommandEventHandler(AudacityLogger::OnClose),
NULL,
this);
mFrame->Disconnect(wxEVT_CLOSE_WINDOW,
wxCloseEventHandler(AudacityLogger::OnCloseWindow),
NULL,
this);
mFrame.reset();
}
}
void AudacityLogger::Show(bool show)
{
// Hide the frame if created, otherwise do nothing
@ -272,7 +223,7 @@ void AudacityLogger::OnCloseWindow(wxCloseEvent & WXUNUSED(e))
// On the Mac, destroy the window rather than hiding it since the
// log menu will override the root windows menu if there is no
// project window open.
Destroy();
mFrame.reset();
#else
Show(false);
#endif

View File

@ -28,10 +28,8 @@
class AudacityLogger final : public wxEvtHandler, public wxLog {
public:
AudacityLogger();
virtual ~AudacityLogger();
void Show(bool show = true);
void Destroy();
#if defined(EXPERIMENTAL_CRASH_REPORT)
wxString GetLog();

View File

@ -152,21 +152,6 @@ HistoryWindow::HistoryWindow(AudacityProject *parent, UndoManager *manager):
this);
}
HistoryWindow::~HistoryWindow()
{
wxTheApp->Disconnect(EVT_AUDIOIO_PLAYBACK,
wxCommandEventHandler(HistoryWindow::OnAudioIO),
NULL,
this);
wxTheApp->Disconnect(EVT_AUDIOIO_CAPTURE,
wxCommandEventHandler(HistoryWindow::OnAudioIO),
NULL,
this);
mAvail->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(HistoryWindow::OnChar));
}
void HistoryWindow::OnAudioIO(wxCommandEvent& evt)
{
evt.Skip();

View File

@ -28,7 +28,6 @@ class HistoryWindow final : public wxDialogWrapper {
public:
HistoryWindow(AudacityProject * parent, UndoManager *manager);
~HistoryWindow();
void UpdateDisplay();

View File

@ -133,14 +133,6 @@ LyricsWindow::LyricsWindow(AudacityProject *parent):
Center();
}
LyricsWindow::~LyricsWindow()
{
mProject->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(LyricsWindow::OnTimer),
NULL,
this);
}
void LyricsWindow::OnCloseWindow(wxCloseEvent & WXUNUSED(event))
{
this->Hide();

View File

@ -23,7 +23,6 @@ class LyricsWindow final : public wxFrame {
public:
LyricsWindow(AudacityProject* parent);
virtual ~LyricsWindow();
LyricsPanel *GetLyricsPanel() { return mLyricsPanel; };

View File

@ -934,17 +934,6 @@ MixerBoard::MixerBoard(AudacityProject* pProject,
this);
}
MixerBoard::~MixerBoard()
{
// private data members
mMusicalInstruments.clear();
mProject->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(MixerBoard::OnTimer),
NULL,
this);
}

View File

@ -206,7 +206,6 @@ public:
wxFrame* parent,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize);
virtual ~MixerBoard();
void UpdatePrefs();

View File

@ -423,7 +423,6 @@ class PluginRegistrationDialog final : public wxDialogWrapper
public:
// constructors and destructors
PluginRegistrationDialog(wxWindow *parent, EffectType type);
virtual ~PluginRegistrationDialog();
private:
void Populate();
@ -501,14 +500,6 @@ PluginRegistrationDialog::PluginRegistrationDialog(wxWindow *parent, EffectType
Populate();
}
PluginRegistrationDialog::~PluginRegistrationDialog()
{
mEffects->Disconnect(wxEVT_KEY_DOWN,
wxKeyEventHandler(PluginRegistrationDialog::OnListChar),
NULL,
this);
}
void PluginRegistrationDialog::Populate()
{
//------------------------- Main section --------------------

View File

@ -1258,11 +1258,6 @@ AudacityProject::~AudacityProject()
mTrackPanel->RemoveOverlay(mCursorOverlay.get());
mTrackPanel->RemoveOverlay(mIndicatorOverlay.get());
}
wxTheApp->Disconnect(EVT_AUDIOIO_CAPTURE,
wxCommandEventHandler(AudacityProject::OnCapture),
NULL,
this);
}
void AudacityProject::ApplyUpdatedTheme()
@ -2715,11 +2710,6 @@ void AudacityProject::OnCloseWindow(wxCloseEvent & event)
#endif
}
this->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(ViewInfo::OnTimer),
NULL,
&mViewInfo);
// Destroys this
pSelf.reset();
mRuler = nullptr;
@ -6038,14 +6028,6 @@ AudacityProject::PlaybackScroller::PlaybackScroller(AudacityProject *project)
this);
}
AudacityProject::PlaybackScroller::~PlaybackScroller()
{
mProject->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(PlaybackScroller::OnTimer),
NULL,
this);
}
void AudacityProject::PlaybackScroller::OnTimer(wxCommandEvent &event)
{
// Let other listeners get the notification

View File

@ -793,7 +793,6 @@ public:
{
public:
explicit PlaybackScroller(AudacityProject *project);
~PlaybackScroller();
enum class Mode {
Off,

View File

@ -770,7 +770,7 @@ Track *SyncLockedTracksIterator::Last(bool skiplinked)
//
// The TrackList sends events whenever certain updates occur to the list it
// is managing. Any other classes that may be interested in get these updates
// should use TrackList::Connect() and TrackList::Disconnect().
// should use TrackList::Connect() or TrackList::Bind().
//
DEFINE_EVENT_TYPE(EVT_TRACKLIST_PERMUTED);
DEFINE_EVENT_TYPE(EVT_TRACKLIST_RESIZING);

View File

@ -360,20 +360,6 @@ TrackPanel::~TrackPanel()
{
mTimer.Stop();
// Unregister for tracklist updates
mTracks->Disconnect(EVT_TRACKLIST_DELETION,
wxCommandEventHandler(TrackPanel::OnTrackListDeletion),
NULL,
this);
mTracks->Disconnect(EVT_TRACKLIST_RESIZING,
wxCommandEventHandler(TrackPanel::OnTrackListResizing),
NULL,
this);
wxTheApp->Disconnect(EVT_AUDIOIO_PLAYBACK,
wxCommandEventHandler(TrackPanel::OnPlayback),
NULL,
this);
// This can happen if a label is being edited and the user presses
// ALT+F4 or Command+Q
if (HasCapture())

View File

@ -43,15 +43,6 @@ public:
this);
}
~TranslatableArray()
{
if (wxTheApp)
wxTheApp->Disconnect(EVT_LANGUAGE_CHANGE,
wxCommandEventHandler(TranslatableArray::Invalidate),
NULL,
this);
}
const ArrayType& Get()
{
if (mContents.empty())

View File

@ -307,14 +307,6 @@ ContrastDialog::ContrastDialog(wxWindow * parent, wxWindowID id,
Center();
}
ContrastDialog::~ContrastDialog()
{
mForegroundRMSText->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(ContrastDialog::OnChar));
mBackgroundRMSText->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(ContrastDialog::OnChar));
mPassFailText->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(ContrastDialog::OnChar));
mDiffText->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(ContrastDialog::OnChar));
}
void ContrastDialog::OnGetURL(wxCommandEvent & WXUNUSED(event))
{
// Original help page is back on-line (March 2016), but the manual should be more reliable.

View File

@ -32,7 +32,6 @@ public:
// constructors and destructors
ContrastDialog(wxWindow * parent, wxWindowID id,
const wxString & title, const wxPoint & pos);
~ContrastDialog();
wxButton * m_pButton_UseCurrentF;
wxButton * m_pButton_UseCurrentB;

View File

@ -3845,16 +3845,6 @@ void EffectUIHost::CleanupRealtime()
{
if (mSupportsRealtime && mInitialized)
{
wxTheApp->Disconnect(EVT_AUDIOIO_PLAYBACK,
wxCommandEventHandler(EffectUIHost::OnPlayback),
NULL,
this);
wxTheApp->Disconnect(EVT_AUDIOIO_CAPTURE,
wxCommandEventHandler(EffectUIHost::OnCapture),
NULL,
this);
EffectManager::Get().RealtimeRemoveEffect(mEffect);
mInitialized = false;

View File

@ -89,37 +89,6 @@ KeyConfigPrefs::KeyConfigPrefs(wxWindow * parent, wxWindowID winid,
}
}
KeyConfigPrefs::~KeyConfigPrefs()
{
if (mKey)
{
mKey->Disconnect(wxEVT_KEY_DOWN,
wxKeyEventHandler(KeyConfigPrefs::OnHotkeyKeyDown),
NULL,
this);
mKey->Disconnect(wxEVT_CHAR,
wxKeyEventHandler(KeyConfigPrefs::OnHotkeyChar),
NULL,
this);
mKey->Disconnect(wxEVT_KILL_FOCUS,
wxFocusEventHandler(KeyConfigPrefs::OnHotkeyKillFocus),
NULL,
this);
}
if (mFilter)
{
mKey->Disconnect(wxEVT_KEY_DOWN,
wxKeyEventHandler(KeyConfigPrefs::OnFilterKeyDown),
NULL,
this);
mKey->Disconnect(wxEVT_CHAR,
wxKeyEventHandler(KeyConfigPrefs::OnFilterChar),
NULL,
this);
}
}
void KeyConfigPrefs::Populate()
{
ShuttleGui S(this, eIsCreatingFromPrefs);

View File

@ -36,7 +36,6 @@ class KeyConfigPrefs final : public PrefsPanel
{
public:
KeyConfigPrefs(wxWindow * parent, wxWindowID winid, const wxString &name);
~KeyConfigPrefs();
bool Commit() override;
void Cancel() override;
wxString HelpPageName() override;

View File

@ -458,30 +458,6 @@ ToolManager::~ToolManager()
// crashing when running with Jaws on Windows 10 1703.
mTopDock->GetConfiguration().Clear();
mBotDock->GetConfiguration().Clear();
// Remove handlers from parent
mParent->Disconnect( wxEVT_LEFT_UP,
wxMouseEventHandler( ToolManager::OnMouse ),
NULL,
this );
mParent->Disconnect( wxEVT_MOTION,
wxMouseEventHandler( ToolManager::OnMouse ),
NULL,
this );
mParent->Disconnect( wxEVT_MOUSE_CAPTURE_LOST,
wxMouseCaptureLostEventHandler( ToolManager::OnCaptureLost ),
NULL,
this );
// Remove our event handlers
mIndicator->Disconnect( wxEVT_CREATE,
wxWindowCreateEventHandler( ToolManager::OnIndicatorCreate ),
NULL,
this );
mIndicator->Disconnect( wxEVT_PAINT,
wxPaintEventHandler( ToolManager::OnIndicatorPaint ),
NULL,
this );
}
// This table describes the default configuration of the toolbars as

View File

@ -117,11 +117,6 @@ PlayIndicatorOverlay::~PlayIndicatorOverlay()
if(ruler)
ruler->RemoveOverlay(mPartner.get());
}
mProject->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(PlayIndicatorOverlay::OnTimer),
NULL,
this);
}
void PlayIndicatorOverlay::OnTimer(wxCommandEvent &event)

View File

@ -212,10 +212,6 @@ Scrubber::~Scrubber()
#endif
mProject->PopEventHandler();
if (wxTheApp)
wxTheApp->Disconnect
(wxEVT_ACTIVATE_APP,
wxActivateEventHandler(Scrubber::OnActivateOrDeactivateApp), NULL, this);
}
namespace {
@ -736,14 +732,6 @@ ScrubbingOverlay::ScrubbingOverlay(AudacityProject *project)
this);
}
ScrubbingOverlay::~ScrubbingOverlay()
{
mProject->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(ScrubbingOverlay::OnTimer),
NULL,
this);
}
std::pair<wxRect, bool> ScrubbingOverlay::DoGetRectangle(wxSize)
{
wxRect rect(mLastScrubRect);

View File

@ -209,7 +209,6 @@ class ScrubbingOverlay final : public wxEvtHandler, public Overlay
{
public:
ScrubbingOverlay(AudacityProject *project);
virtual ~ScrubbingOverlay();
private:
std::pair<wxRect, bool> DoGetRectangle(wxSize size) override;

View File

@ -1048,15 +1048,6 @@ public:
this);
}
~TimerHandler()
{
if (mConnectedProject)
mConnectedProject->Disconnect(EVT_TRACK_PANEL_TIMER,
wxCommandEventHandler(SelectHandle::TimerHandler::OnTimer),
NULL,
this);
}
// Receives timer event notifications, to implement auto-scroll
void OnTimer(wxCommandEvent &event);

View File

@ -340,35 +340,6 @@ void MeterPanel::Clear()
mQueue.Clear();
}
MeterPanel::~MeterPanel()
{
if (mIsInput)
{
// Unregister for AudioIO events
wxTheApp->Disconnect(EVT_AUDIOIO_MONITOR,
wxCommandEventHandler(MeterPanel::OnAudioIOStatus),
NULL,
this);
wxTheApp->Disconnect(EVT_AUDIOIO_CAPTURE,
wxCommandEventHandler(MeterPanel::OnAudioIOStatus),
NULL,
this);
}
else
{
wxTheApp->Disconnect(EVT_AUDIOIO_PLAYBACK,
wxCommandEventHandler(MeterPanel::OnAudioIOStatus),
NULL,
this);
}
// Unregister for our preference update event
wxTheApp->Disconnect(EVT_METER_PREFERENCES_CHANGED,
wxCommandEventHandler(MeterPanel::OnMeterPrefsUpdated),
NULL,
this);
}
void MeterPanel::UpdatePrefs()
{
mDBRange = gPrefs->Read(ENV_DB_KEY, ENV_DB_RANGE);

View File

@ -114,8 +114,6 @@ class MeterPanel final : public wxPanelWrapper
Style style = HorizontalStereo,
float fDecayRate = 60.0f);
~MeterPanel();
bool AcceptsFocus() const override { return s_AcceptsFocus; }
bool AcceptsFocusFromKeyboard() const override { return true; }

View File

@ -13,6 +13,8 @@ Paul Licameli split from TrackPanel.cpp
PopupMenuTable::Menu::~Menu()
{
// Event connections between the parent window and the singleton table
// object must be broken when this menu is destroyed.
Disconnect();
}

View File

@ -2031,11 +2031,6 @@ AdornedRulerPanel::~AdornedRulerPanel()
{
if(HasCapture())
ReleaseMouse();
wxTheApp->Disconnect(EVT_AUDIOIO_CAPTURE,
wxCommandEventHandler(AdornedRulerPanel::OnCapture),
NULL,
this);
}
#if 1

View File

@ -27,11 +27,6 @@ public:
{
this->Bind(wxEVT_CHAR_HOOK, wxTabTraversalWrapperCharHook);
}
~wxTabTraversalWrapper()
{
this->Unbind(wxEVT_CHAR_HOOK, wxTabTraversalWrapperCharHook);
}
};
class AUDACITY_DLL_API wxPanelWrapper : public wxTabTraversalWrapper<wxPanel>