LibGUI: Fix GDirectoryModel lifetime bug.

Thumbnail generation callbacks were getting called after the class was already being destroyed causing a crash to occur.
This commit is contained in:
Brandon Scott 2019-10-01 20:49:10 -05:00 committed by Andreas Kling
parent 9da121f837
commit 17597f4681
Notes: sideshowbarker 2024-07-19 11:51:12 +09:00
2 changed files with 9 additions and 2 deletions

View file

@ -126,14 +126,20 @@ bool GDirectoryModel::fetch_thumbnail_for(const Entry& entry)
s_thumbnail_cache.set(path, nullptr);
m_thumbnail_progress_total++;
auto directory_model = make_weak_ptr();
LibThread::BackgroundAction<RefPtr<GraphicsBitmap>>::create(
[path] {
return render_thumbnail(path);
},
[this, path](auto thumbnail) {
[this, path, directory_model](auto thumbnail) {
s_thumbnail_cache.set(path, move(thumbnail));
// class was destroyed, no need to update progress or call any event handlers.
if (directory_model.is_null())
return;
m_thumbnail_progress++;
if (on_thumbnail_progress)
on_thumbnail_progress(m_thumbnail_progress, m_thumbnail_progress_total);

View file

@ -5,7 +5,8 @@
#include <LibGUI/GModel.h>
#include <sys/stat.h>
class GDirectoryModel final : public GModel {
class GDirectoryModel final : public GModel
, public Weakable<GDirectoryModel> {
public:
static NonnullRefPtr<GDirectoryModel> create() { return adopt(*new GDirectoryModel); }
virtual ~GDirectoryModel() override;