FIX: use thread stack to keep shared ref of PrinterFileSystem

Change-Id: I86dce263f43eb65074d4fec777cb08885505b7cf
(cherry picked from commit 1dbc32b2335c33831ac94a787c8e7ec7d898ccba)
This commit is contained in:
chunmao.guo 2022-09-27 13:11:01 +08:00 committed by Lane.Wei
parent e228e72db1
commit 35bb20b2cf
5 changed files with 19 additions and 11 deletions

View File

@ -6024,8 +6024,10 @@ private:
boost::posix_time::ptime start;
};
private:
_BBS_Backup_Manager() : m_thread(boost::ref(*this)) {
_BBS_Backup_Manager() {
m_next_backup = boost::get_system_time() + boost::posix_time::seconds(m_interval);
boost::unique_lock lock(m_mutex);
m_thread = std::move(boost::thread(boost::ref(*this)));
}
~_BBS_Backup_Manager() {

View File

@ -220,6 +220,7 @@ void MediaFilePanel::SetMachineObject(MachineObject* obj)
m_image_grid->SetStatus(m_bmp_failed.bmp(), _L("Not supported."));
} else {
boost::shared_ptr<PrinterFileSystem> fs(new PrinterFileSystem);
fs->Attached();
m_image_grid->SetFileType(m_last_type);
m_image_grid->SetFileSystem(fs);
fs->Bind(EVT_MODE_CHANGED, &MediaFilePanel::modeChanged, this);

View File

@ -227,11 +227,6 @@ void MonitorPanel::set_default()
/* reset side tool*/
//m_bitmap_wifi_signal->SetBitmap(wxNullBitmap);
#if !BBL_RELEASE_TO_PUBLIC
/* reset time lapse panel */
m_media_file_panel->SetMachineObject(nullptr);
#endif
wxGetApp().sidebar().load_ams_list({});
}

View File

@ -27,7 +27,6 @@ struct StaticBambuLib : BambuLib {
PrinterFileSystem::PrinterFileSystem()
: BambuLib(StaticBambuLib::get())
, m_recv_thread(&PrinterFileSystem::RecvMessageThread, this)
{
if (!default_thumbnail.IsOk())
default_thumbnail = wxImage(Slic3r::encode_path(Slic3r::var("live_stream_default.png").c_str()));
@ -41,7 +40,9 @@ PrinterFileSystem::PrinterFileSystem()
}
PrinterFileSystem::~PrinterFileSystem()
{ m_recv_thread.detach(); }
{
m_recv_thread.detach();
}
void PrinterFileSystem::SetFileType(FileType type)
{
@ -251,6 +252,15 @@ int PrinterFileSystem::RecvData(std::function<int(Bambu_Sample& sample)> const &
return result;
}
void PrinterFileSystem::Attached()
{
boost::unique_lock lock(m_mutex);
m_recv_thread = std::move(boost::thread([w = weak_from_this()] {
boost::shared_ptr<PrinterFileSystem> s = w.lock();
if (s) s->RecvMessageThread();
}));
}
void PrinterFileSystem::Start()
{
boost::unique_lock l(m_mutex);
@ -277,8 +287,6 @@ void PrinterFileSystem::Stop(bool quit)
boost::unique_lock l(m_mutex);
if (quit) {
m_session.owner = nullptr;
// let the thread delete this
m_callbacks.push_back([thiz = shared_from_this()](int result, json const &, unsigned char const *) { (void) thiz; });
} else if (m_stopped) {
return;
}
@ -624,7 +632,7 @@ void PrinterFileSystem::RecvMessageThread()
if (m_stopped && (m_session.owner == nullptr || (m_messages.empty() && m_callbacks.empty()))) {
Reconnect(l, 0); // Close and wait start again
if (m_session.owner == nullptr) {
// clear callbacks may invoke destructor, so clear first
// clear callbacks first
auto callbacks(std::move(m_callbacks));
break;
}

View File

@ -149,6 +149,8 @@ public:
Status GetStatus() const { return m_status; }
int GetLastError() const { return m_last_error; }
void Attached();
void Start();
void Retry();