Skip to content

Commit 42b6558

Browse files
committed
Partially fix tab close memory leak
1 parent e6eb056 commit 42b6558

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

lector/__main__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os
20+
import gc
2021
import sys
2122
import hashlib
2223
import pathlib
@@ -526,7 +527,7 @@ def ifcontinue(box_button):
526527

527528
# Update the database in the background
528529
self.thread = BackGroundBookDeletion(
529-
delete_hashes, self.database_path, self)
530+
delete_hashes, self.database_path)
530531
self.thread.finished.connect(self.move_on)
531532
self.thread.start()
532533

@@ -647,7 +648,10 @@ def tab_close(self, tab_index=None):
647648
self.thread.start()
648649

649650
self.tabWidget.widget(tab_index).update_last_accessed_time()
650-
self.tabWidget.removeTab(tab_index)
651+
652+
self.tabWidget.widget(tab_index).deleteLater()
653+
self.tabWidget.widget(tab_index).setParent(None)
654+
gc.collect()
651655

652656
def set_toc_position(self, event=None):
653657
current_tab = self.tabWidget.widget(self.tabWidget.currentIndex())
@@ -773,7 +777,7 @@ def finishing_touches():
773777
# New tabs are created here
774778
# Initial position adjustment is carried out by the tab itself
775779
file_data = contents[i]
776-
Tab(file_data, self.tabWidget)
780+
Tab(file_data, self)
777781

778782
if self.settings['last_open_tab'] == 'library':
779783
self.tabWidget.setCurrentIndex(0)

lector/settingsdialog.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
class SettingsUI(QtWidgets.QDialog, settingswindow.Ui_Dialog):
35-
def __init__(self, parent):
35+
def __init__(self, parent=None):
3636
super(SettingsUI, self).__init__()
3737
self.setupUi(self)
3838
self._translate = QtCore.QCoreApplication.translate
@@ -216,7 +216,7 @@ def start_library_scan(self):
216216
# Traverse directories looking for files
217217
self.parent.statusMessage.setText(
218218
self._translate('SettingsUI', 'Checking library folders'))
219-
self.thread = BackGroundBookSearch(data_pairs, self)
219+
self.thread = BackGroundBookSearch(data_pairs)
220220
self.thread.finished.connect(self.finished_iterating)
221221
self.thread.start()
222222

lector/threaded.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def run(self):
4444

4545

4646
class BackGroundBookAddition(QtCore.QThread):
47-
def __init__(self, file_list, database_path, addition_mode, parent=None):
47+
def __init__(self, file_list, database_path, addition_mode, main_window, parent=None):
4848
super(BackGroundBookAddition, self).__init__(parent)
4949
self.file_list = file_list
50-
self.parent = parent
5150
self.database_path = database_path
5251
self.addition_mode = addition_mode
52+
self.main_window = main_window
5353

5454
self.prune_required = True
5555
if self.addition_mode == 'manual':
@@ -60,21 +60,20 @@ def run(self):
6060
self.file_list,
6161
('addition', self.addition_mode),
6262
self.database_path,
63-
self.parent.settings['auto_tags'],
64-
self.parent.temp_dir.path())
63+
self.main_window.settings['auto_tags'],
64+
self.main_window.temp_dir.path())
6565

6666
parsed_books = books.initiate_threads()
67-
self.parent.lib_ref.generate_model('addition', parsed_books, False)
67+
self.main_window.lib_ref.generate_model('addition', parsed_books, False)
6868
database.DatabaseFunctions(self.database_path).add_to_database(parsed_books)
6969

7070
if self.prune_required:
71-
self.parent.lib_ref.prune_models(self.file_list)
71+
self.main_window.lib_ref.prune_models(self.file_list)
7272

7373

7474
class BackGroundBookDeletion(QtCore.QThread):
7575
def __init__(self, hash_list, database_path, parent=None):
7676
super(BackGroundBookDeletion, self).__init__(parent)
77-
self.parent = parent
7877
self.hash_list = hash_list
7978
self.database_path = database_path
8079

@@ -86,7 +85,6 @@ def run(self):
8685
class BackGroundBookSearch(QtCore.QThread):
8786
def __init__(self, data_list, parent=None):
8887
super(BackGroundBookSearch, self).__init__(parent)
89-
self.parent = parent
9088
self.valid_files = []
9189

9290
# Filter for checked directories

lector/widgets.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@
4141

4242

4343
class Tab(QtWidgets.QWidget):
44-
def __init__(self, metadata, parent=None):
44+
def __init__(self, metadata, main_window, parent=None):
4545
super(Tab, self).__init__(parent)
4646
self._translate = QtCore.QCoreApplication.translate
4747

48-
self.parent = parent
48+
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
49+
50+
self.main_window = main_window
4951
self.metadata = metadata # Save progress data into this dictionary
5052
self.are_we_doing_images_only = self.metadata['images_only']
5153
self.is_fullscreen = False
52-
self.main_window = self.window()
5354

5455
self.masterLayout = QtWidgets.QHBoxLayout(self)
5556
self.masterLayout.setContentsMargins(0, 0, 0, 0)
@@ -152,7 +153,7 @@ def __init__(self, metadata, parent=None):
152153
self.horzLayout.addWidget(self.contentView)
153154
self.horzLayout.addWidget(self.dockWidget)
154155
title = self.metadata['title']
155-
self.parent.addTab(self, title)
156+
self.main_window.tabWidget.addTab(self, title)
156157

157158
# Hide mouse cursor timer
158159
self.mouse_hide_timer = QtCore.QTimer()

0 commit comments

Comments
 (0)