Skip to content

Commit

Permalink
Merge pull request #2962 from ales-erjavec/fixes/controlled-list-memleak
Browse files Browse the repository at this point in the history
[FIX] gui.ControlledList: Take a weakref to listBox
  • Loading branch information
lanzagar authored Mar 30, 2018
2 parents b1972e5 + f1e4128 commit 68346c2
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions Orange/widgets/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import itertools
import warnings
import logging
import weakref
from types import LambdaType
from collections import defaultdict, Sequence

Expand Down Expand Up @@ -2063,7 +2064,14 @@ class ControlledList(list):
"""
def __init__(self, content, listBox=None):
super().__init__(content if content is not None else [])
self.listBox = listBox
# Controlled list is created behind the back by gui.listBox and
# commonly used as a setting which gets synced into a GLOBAL
# SettingsHandler and which keeps the OWWidget instance alive via a
# reference in listBox (see gui.listBox)
if listBox is not None:
self.listBox = weakref.ref(listBox)
else:
self.listBox = lambda: None

def __reduce__(self):
# cannot pickle self.listBox, but can't discard it
Expand All @@ -2073,7 +2081,7 @@ def __reduce__(self):

# TODO ControllgedList.item2name is probably never used
def item2name(self, item):
item = self.listBox.labels[item]
item = self.listBox().labels[item]
if isinstance(item, tuple):
return item[1]
else:
Expand All @@ -2082,12 +2090,12 @@ def item2name(self, item):
def __setitem__(self, index, item):
def unselect(i):
try:
item = self.listBox.item(i)
item = self.listBox().item(i)
except RuntimeError: # Underlying C/C++ object has been deleted
item = None
if item is None:
# Labels changed before clearing the selection: clear everything
self.listBox.selectionModel().clear()
self.listBox().selectionModel().clear()
else:
item.setSelected(0)

Expand All @@ -2098,15 +2106,15 @@ def unselect(i):
for i in self[index]:
unselect(i)
for i in item:
self.listBox.item(i).setSelected(1)
self.listBox().item(i).setSelected(1)
super().__setitem__(index, item)

def __delitem__(self, index):
if isinstance(index, int):
self.listBox.item(self[index]).setSelected(0)
self.listBox().item(self[index]).setSelected(0)
else:
for i in self[index]:
self.listBox.item(i).setSelected(0)
self.listBox().item(i).setSelected(0)
super().__delitem__(index)

def append(self, item):
Expand All @@ -2116,15 +2124,15 @@ def append(self, item):
def extend(self, items):
super().extend(items)
for i in items:
self.listBox.item(i).setSelected(1)
self.listBox().item(i).setSelected(1)

def insert(self, index, item):
item.setSelected(1)
super().insert(index, item)

def pop(self, index=-1):
i = super().pop(index)
self.listBox.item(i).setSelected(0)
self.listBox().item(i).setSelected(0)

def remove(self, item):
item.setSelected(0)
Expand Down

0 comments on commit 68346c2

Please sign in to comment.