Repository: qarmin/czkawka (language: rust)

Issue:
Sometimes crashes/freezes when you select checkbox in result and try to scan again with a lot of data
**Desktop (please complete the following information):**

- Czkawka version<!--  e.g. 6.1.0 cli/gui -->: 6.1.0
- OS version<!--  e.g Ubuntu 22.04, Windows 11, Mac 15.1 ARM -->: Fedora 38 Silverblue, but run in flatpak 
- Terminal output[optional]:

<details>
<summary>Debug log</summary>

```
17:44:28.671 [INFO] czkawka_core::duplicate: find_duplicates
17:44:28.671 [DEBUG] czkawka_core::duplicate: check_files_size
17:44:28.671 [DEBUG] czkawka_core::common_dir_traversal: run(collecting files/dirs)
17:44:31.204 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:31.206 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 1.05ms
17:44:31.208 [DEBUG] czkawka_core::common_dir_traversal: Collected 178727 files, 0 folders
17:44:31.208 [DEBUG] czkawka_core::common_dir_traversal: run(collecting files/dirs): Done in 2.54s
17:44:32.690 [DEBUG] czkawka_core::duplicate: filter_reference_folders_by_size
17:44:32.690 [DEBUG] czkawka_core::duplicate: filter_reference_folders_by_size: Done in 1.89µs
17:44:32.692 [DEBUG] czkawka_core::duplicate: check_file_size - after calculating size stats/duplicates, found in 25371 groups, 115423 files with same size | referenced 0 groups, 0 files
17:44:32.692 [DEBUG] czkawka_core::duplicate: check_files_size: Done in 4.02s
17:44:32.692 [DEBUG] czkawka_core::duplicate: check_files_hash
17:44:32.692 [DEBUG] czkawka_core::duplicate: prehashing
17:44:32.692 [DEBUG] czkawka_core::duplicate: prehash_load_cache_at_start
17:44:32.692 [DEBUG] czkawka_core::duplicate: prehash_load_cache_at_start: Done in 1.47µs
17:44:32.692 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:32.692 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 169.93µs
17:44:32.693 [DEBUG] czkawka_core::duplicate: Starting calculating prehash
17:44:35.280 [DEBUG] czkawka_core::duplicate: Completed calculating prehash
17:44:35.280 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:35.283 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 2.19ms
17:44:35.318 [DEBUG] czkawka_core::duplicate: prehash_save_cache_at_exit
17:44:35.318 [DEBUG] czkawka_core::duplicate: prehash_save_cache_at_exit: Done in 3.35µs
17:44:35.318 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:35.323 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 4.90ms
17:44:35.391 [DEBUG] czkawka_core::duplicate: prehashing: Done in 2.70s
17:44:35.391 [DEBUG] czkawka_core::duplicate: full_hashing
17:44:35.391 [DEBUG] czkawka_core::duplicate: full_hashing_load_cache_at_start
17:44:35.391 [DEBUG] czkawka_core::duplicate: full_hashing_load_cache_at_start - using cache
17:44:35.391 [DEBUG] czkawka_core::common_cache: load_cache_from_file_generalized_by_size
17:44:35.391 [DEBUG] czkawka_core::common_cache: Converting cache BtreeMap<u64, Vec<T>> into BTreeMap<String, T>
17:44:35.517 [DEBUG] czkawka_core::common_cache: Converted cache BtreeMap<u64, Vec<T>> into BTreeMap<String, T>
17:44:35.517 [DEBUG] czkawka_core::common_cache: load_cache_from_file_generalized
17:44:35.522 [DEBUG] czkawka_core::common_cache: Starting removing outdated cache entries (removing non existent files from cache - true)
17:44:35.526 [DEBUG] czkawka_core::common_cache: Completed removing outdated cache entries, removed 0 out of all 4474 entries
17:44:35.526 [DEBUG] czkawka_core::common_cache: Loaded cache from file cache_duplicates_Blake3_61.bin (or json alternative) - 4474 results
17:44:35.526 [DEBUG] czkawka_core::common_cache: load_cache_from_file_generalized: Done in 9.41ms
17:44:35.526 [DEBUG] czkawka_core::common_cache: Converting cache Vec<T> into BTreeMap<u64, Vec<T>>
17:44:35.528 [DEBUG] czkawka_core::common_cache: Converted cache Vec<T> into BTreeMap<u64, Vec<T>>
17:44:35.565 [DEBUG] czkawka_core::common_cache: load_cache_from_file_generalized_by_size: Done in 174.46ms
17:44:35.565 [DEBUG] czkawka_core::duplicate: full_hashing_load_cache_at_start - started diff between loaded and prechecked files
17:44:35.578 [DEBUG] czkawka_core::duplicate: full_hashing_load_cache_at_start - completed diff between loaded and prechecked files - 11800(12.93 GiB) non cached, 2054(9.20 GiB) already cached
17:44:35.578 [DEBUG] czkawka_core::duplicate: full_hashing_load_cache_at_start: Done in 187.28ms
17:44:35.578 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:35.592 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 14.45ms
17:44:35.593 [DEBUG] czkawka_core::duplicate: Starting full hashing of 66166 files
17:44:45.739 [DEBUG] czkawka_core::duplicate: Finished full hashing
17:44:45.739 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:45.755 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 15.87ms
17:44:45.755 [DEBUG] czkawka_core::duplicate: full_hashing_save_cache_at_exit
17:44:45.891 [DEBUG] czkawka_core::common_cache: save_cache_to_file_generalized
17:44:45.898 [DEBUG] czkawka_core::common_cache: Saved binary to file "/var/home/rugk/.var/app/com.github.qarmin.czkawka/cache/czkawka/cache_duplicates_Blake3_61.bin"
17:44:45.898 [DEBUG] czkawka_core::common_cache: Properly saved to file 70640 cache entries.
17:44:45.898 [DEBUG] czkawka_core::common_cache: save_cache_to_file_generalized: Done in 7.05ms
17:44:45.918 [DEBUG] czkawka_core::duplicate: full_hashing_save_cache_at_exit: Done in 162.47ms
17:44:45.918 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads
17:44:45.937 [DEBUG] czkawka_core::common: send_info_and_wait_for_ending_all_threads: Done in 18.80ms
17:44:45.958 [DEBUG] czkawka_core::duplicate: full_hashing: Done in 10.57s
17:44:45.958 [DEBUG] czkawka_core::duplicate: hash_reference_folders
17:44:45.959 [DEBUG] czkawka_core::duplicate: hash_reference_folders: Done in 664.34µs
17:44:45.959 [DEBUG] czkawka_core::duplicate: check_files_hash: Done in 13.27s
17:44:45.959 [DEBUG] czkawka_core::duplicate: delete_files
17:44:45.959 [DEBUG] czkawka_core::duplicate: delete_files: Done in 1.96µs
17:44:45.959 [INFO] czkawka_core::duplicate: find_duplicates: Done in 17.29s
17:44:45.961 [DEBUG] czkawka_gui::compute_results: computer_duplicate_finder
17:44:46.602 [DEBUG] czkawka_gui::compute_results: computer_duplicate_finder: Done in 640.78ms
17:44:55.438 [DEBUG] czkawka_gui::connect_things::connect_button_search: clean_tree_view
```

</details>

**Bug Description**
From what I see, it seems to freeze/crash when `clean_tree_view` is called, i.e. cleaning the tree view seems problematic for many entries…

## STR

1. Start scan.
2. I don't know exactly what is needed, but interact with the results list seems to work, i.e. click a checkbox somewhere.
3. Now start a scan again.

Another requirements seems to be that _many_ results are being found (i.e. when I searched with a subset of the files I could not reproduce it).

### What happens

The GUI freezes and I need to force quit the application. Note that it happens instantly (see video below), so the issue seems to not be the search itself (I don't even see the progress window), but the GUI.

![image](https://github.com/qarmin/czkawka/assets/11966684/de9cfa37-ba41-4d30-a245-5b3d292061b7)

> ⁨53279⁩ Duplikate in ⁨15422⁩ Gruppen gefunden, die ⁨14.79 GiB⁩ benötigt haben

i.e. im English: ⁨53279⁩ dupes in ⁨15422⁩ groups, 15 GiB

```
############### NACHRICHT(2) ###############
Properly loaded 9699 cache entries.
Properly saved to file 69752 cache entries.
```

[Bildschirmaufzeichnung vom 2023-10-24, 17-32-10.webm](https://github.com/qarmin/czkawka/assets/11966684/c7120b4e-13a9-4873-8dac-3c728f894562)


### What should happen

Not freeze.

## System

```
$ flatpak info com.github.qarmin.czkawka 

Czkawka - Multi functional app to find duplicates, empty folders, similar
images, broken files etc.

         Kennung: com.github.qarmin.czkawka
             Ref: app/com.github.qarmin.czkawka/x86_64/stable
     Architektur: x86_64
           Zweig: stable
         Version: 6.1.0
          Lizenz: MIT
        Ursprung: flathub
        Sammlung: org.flathub.Stable
    Installation: system
     Installiert: 17,2 MB
Laufzeitumgebung: org.gnome.Platform/x86_64/45
             Sdk: org.gnome.Sdk/x86_64/45

          Commit: eac69a7ec3837f2acbb85290c6ed60054a33f9761e02f5f522bad107acdcfaa5
          Parent: 7c527a356b9775e006bdd6494cffe76215da47b28cd0c7bc55ce530bca4354d9
         Betreff: 6.1 update (#23) (1f1466fd)
           Datum: 2023-10-15 08:39:26 +0000
```


Discussion / hints:
This is gtk bug, and looks that gtk creators not have any plans to fix problem, because it happens with deprecacted TreeView component

https://gitlab.gnome.org/GNOME/gtk/-/issues/6136

Ugly and hacky solution is to remove all liststore items(via iters) in reverse - from simple test look like this works fine but is 50% slower(but still is this better than random freezing)

Fix patch:
diff --git a/czkawka_gui/src/connect_things/connect_button_search.rs b/czkawka_gui/src/connect_things/connect_button_search.rs
index 603f43c0d..72c478467 100644
--- a/czkawka_gui/src/connect_things/connect_button_search.rs
+++ b/czkawka_gui/src/connect_things/connect_button_search.rs
@@ -830,5 +830,18 @@ fn bad_extensions_search(
 #[fun_time(message = "clean_tree_view", level = "debug")]
 fn clean_tree_view(tree_view: &gtk4::TreeView) {
     let list_store = get_list_store(tree_view);
-    list_store.clear();
+    let mut all_iters = Vec::new();
+    let first_iter = list_store.iter_first();
+    if let Some(first_iter) = first_iter {
+        loop {
+            all_iters.push(first_iter);
+            if !list_store.iter_next(&first_iter) {
+                break;
+            }
+        }
+    }
+    all_iters.reverse();
+    for iter in all_iters {
+        list_store.remove(&iter);
+    }
 }


Test patch: