Skip to content

Commit

Permalink
Fix issue causing FreeDV to segfault on exit when RADE is running. (#814
Browse files Browse the repository at this point in the history
)

* Fix issue causing FreeDV to segfault on exit when RADE is running.

* Add logic to go through all the stop steps on exit as though the user pushed the Stop button.

* Move AudioEngine shutdown to destructor.

* Simplify exit logic and defer form destroy until after modem stop.

* Don't skip the event on window close or else wxWidgets will destroy twice.
  • Loading branch information
tmiw authored Jan 18, 2025
1 parent c1cc52e commit d0d734a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
89 changes: 52 additions & 37 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,8 @@ MainFrame::MainFrame(wxWindow *parent) : TopFrame(parent, wxID_ANY, _("FreeDV ")
pthread_setname_np(pthread_self(), "FreeDV GUI");
#endif // defined(__linux__)

terminating_ = false;

// Add config file name to title bar if provided at the command line.
if (wxGetApp().customConfigFileName != "")
{
Expand Down Expand Up @@ -1424,6 +1426,7 @@ MainFrame::~MainFrame()
if (m_RxRunning)
{
stopRxStream();
freedvInterface.stop();
}
sox_biquad_finish();

Expand Down Expand Up @@ -1469,6 +1472,10 @@ MainFrame::~MainFrame()

// Clean up RADE.
rade_finalize();

auto engine = AudioEngineFactory::GetAudioEngine();
engine->stop();
engine->setOnEngineError(nullptr, nullptr);
}


Expand Down Expand Up @@ -2147,52 +2154,55 @@ void MainFrame::OnTimer(wxTimerEvent &evt)
}
#endif

void MainFrame::topFrame_OnClose( wxCloseEvent& event )
{
if (m_RxRunning)
{
if (m_btnTogPTT->GetValue())
{
// Stop PTT first
togglePTT();
}

// Stop execution.
terminating_ = true;
wxCommandEvent* offEvent = new wxCommandEvent(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_togBtnOnOff->GetId());
offEvent->SetEventObject(m_togBtnOnOff);
m_togBtnOnOff->SetValue(false);
OnTogBtnOnOff(*offEvent);
delete offEvent;
}
else
{
TopFrame::topFrame_OnClose(event);
}
}

//-------------------------------------------------------------------------
// OnExit()
//-------------------------------------------------------------------------
void MainFrame::OnExit(wxCommandEvent& event)
{
if (wxGetApp().rigFrequencyController)
{
wxGetApp().rigFrequencyController->disconnect();
wxGetApp().rigFrequencyController = nullptr;
}

if (wxGetApp().rigPttController)
{
wxGetApp().rigPttController->disconnect();
wxGetApp().rigPttController = nullptr;
}

wxGetApp().m_reporters.clear();

wxUnusedVar(event);
#ifdef _USE_TIMER
m_plotTimer.Stop();
m_pskReporterTimer.Stop();
#endif // _USE_TIMER
if(g_sfPlayFile != NULL)
{
sf_close(g_sfPlayFile);
g_sfPlayFile = NULL;
}
if(g_sfRecFile != NULL)
if (m_RxRunning)
{
sf_close(g_sfRecFile);
g_sfRecFile = NULL;
}
if(m_RxRunning)
if (m_btnTogPTT->GetValue())
{
// Stop PTT first
togglePTT();
}

// Stop execution.
terminating_ = true;
wxCommandEvent* offEvent = new wxCommandEvent(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_togBtnOnOff->GetId());
offEvent->SetEventObject(m_togBtnOnOff);
m_togBtnOnOff->SetValue(false);
OnTogBtnOnOff(*offEvent);
delete offEvent;
}
else
{
stopRxStream();
Destroy();
}
m_togBtnAnalog->Disable();

auto engine = AudioEngineFactory::GetAudioEngine();
engine->stop();
engine->setOnEngineError(nullptr, nullptr);

Destroy();
}

void MainFrame::OnChangeTxMode( wxCommandEvent& event )
Expand Down Expand Up @@ -2782,6 +2792,11 @@ void MainFrame::OnTogBtnOnOff(wxCommandEvent& event)
m_togBtnOnOff->SetValue(m_RxRunning);
m_togBtnOnOff->SetLabel(wxT("&Start"));
m_togBtnOnOff->Enable(true);

if (terminating_)
{
CallAfter([&]() { Destroy(); });
}
});
});
onOffExec.detach();
Expand Down
3 changes: 3 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class MainFrame : public TopFrame

// protected event handlers
virtual void topFrame_OnSize( wxSizeEvent& event ) override;
virtual void topFrame_OnClose( wxCloseEvent& event ) override;
virtual void OnCloseFrame(wxCloseEvent& event);
void OnExitClick(wxCommandEvent& event);

Expand Down Expand Up @@ -493,6 +494,8 @@ class MainFrame : public TopFrame
wxMenuItem* adjustMonitorVKVolMenuItem_;
wxMenuItem* chooseVKFileMenuItem_;
wxMenuItem* recordNewVoiceKeyerFileMenuItem_;

bool terminating_; // used for terminating FreeDV

int getSoundCardIDFromName(wxString& name, bool input);
bool validateSoundCardSetup();
Expand Down

0 comments on commit d0d734a

Please sign in to comment.