From 3e4cefb04fc9e597c81b9d9997ddebf0e178d0e4 Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Wed, 12 Jan 2022 17:39:59 +0400 Subject: [PATCH] Fixed a crash when trying to render main window icons which have incomplete data; fixes #13 in main branch. --- src/gsc_main_window.cpp | 4 ++++ src/gsc_main_window_iconview.h | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/gsc_main_window.cpp b/src/gsc_main_window.cpp index 1bebc057..d0675352 100644 --- a/src/gsc_main_window.cpp +++ b/src/gsc_main_window.cpp @@ -1187,6 +1187,10 @@ bool GscMainWindow::testing_active() const std::shared_ptr GscMainWindow::show_device_info_window(const StorageDevicePtr& drive) { + if (!drive) { + return nullptr; + } + // if a test is being run on it, disallow. if (drive->get_test_is_active()) { gui_show_warn_dialog(_("Please wait until the test is finished on this drive."), this); diff --git a/src/gsc_main_window_iconview.h b/src/gsc_main_window_iconview.h index e0a7afc0..8e5610e5 100644 --- a/src/gsc_main_window_iconview.h +++ b/src/gsc_main_window_iconview.h @@ -83,6 +83,8 @@ class GscMainWindowIconView : public Gtk::IconView { columns.add(col_drive_ptr); + columns.add(col_populated); + // create a Tree Model ref_list_model = Gtk::ListStore::create(columns); // ref_list_model->set_sort_column(col_name, Gtk::SORT_ASCENDING); @@ -232,6 +234,8 @@ class GscMainWindowIconView : public Gtk::IconView { this->decorate_entry(row); + row[col_populated] = true; // triggers rendering + drive->signal_changed().connect(sigc::mem_fun(this, &GscMainWindowIconView::on_drive_changed)); if (scroll_to_it) { @@ -270,6 +274,9 @@ class GscMainWindowIconView : public Gtk::IconView { void decorate_entry(Gtk::TreeModel::Row& row) { StorageDevicePtr drive = row[col_drive_ptr]; + if (!drive) { + return; + } // it needs this space to be symmetric (why?); std::string name; // = "" + drive->get_device_with_type() + " \n"; @@ -467,8 +474,11 @@ class GscMainWindowIconView : public Gtk::IconView { } else { // enable drives menu, set proper smart toggles Gtk::TreePath model_path = *(this->get_selected_items().begin()); Gtk::TreeModel::Row row = *(ref_list_model->get_iter(model_path)); - StorageDevicePtr drive = row[col_drive_ptr]; + if (!row[col_populated]) { // protect against using incomplete model entry + return; + } + StorageDevicePtr drive = row[col_drive_ptr]; main_window->set_drive_menu_status(drive); } } @@ -482,8 +492,11 @@ class GscMainWindowIconView : public Gtk::IconView { return; Gtk::TreeModel::Row row = *(ref_list_model->get_iter(model_path)); - StorageDevicePtr drive = row[col_drive_ptr]; + if (!row[col_populated]) { // protect against using incomplete model entry + return; + } + StorageDevicePtr drive = row[col_drive_ptr]; main_window->show_device_info_window(drive); } @@ -562,6 +575,7 @@ class GscMainWindowIconView : public Gtk::IconView { Gtk::TreeModelColumn col_description; ///< Model column Gtk::TreeModelColumn > col_pixbuf; ///< Model column Gtk::TreeModelColumn col_drive_ptr; ///< Model column + Gtk::TreeModelColumn col_populated; ///< Model column, indicates whether the model entry has been fully populated. Glib::RefPtr ref_list_model; ///< The icon view model int num_icons = 0; ///< Track the number of icons, because liststore makes it difficult to count them.