Skip to content

Commit e297787

Browse files
committed
Merge pull request #90 from mluessi/fix_add_data_leak
FIX: memory leak when calling add_data in loop
2 parents 9dcf909 + ab09d9c commit e297787

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

surfer/viz.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def add_data(self, array, min=None, max=None, thresh=None,
768768
colormap="RdBu_r", alpha=1,
769769
vertices=None, smoothing_steps=20, time=None,
770770
time_label="time index=%d", colorbar=True,
771-
hemi=None):
771+
hemi=None, remove_existing=False):
772772
"""Display data from a numpy array on the surface.
773773
774774
This provides a similar interface to add_overlay, but it displays
@@ -817,6 +817,9 @@ def add_data(self, array, min=None, max=None, thresh=None,
817817
If None, it is assumed to belong to the hemipshere being
818818
shown. If two hemispheres are being shown, an error will
819819
be thrown.
820+
remove_existing : bool
821+
Remove surface added by previous "add_data" call. Useful for
822+
conserving memory when displaying different data in a loop.
820823
"""
821824
hemi = self._check_hemi(hemi)
822825

@@ -895,6 +898,11 @@ def add_data(self, array, min=None, max=None, thresh=None,
895898
data['surfaces'] = surfs
896899
data['colorbars'] = bars
897900
data['orig_ctable'] = ct
901+
902+
if remove_existing and self.data_dict[hemi] is not None:
903+
for surf in self.data_dict[hemi]['surfaces']:
904+
surf.parent.parent.remove()
905+
898906
self.data_dict[hemi] = data
899907

900908
def add_annotation(self, annot, borders=True, alpha=1, hemi=None,

0 commit comments

Comments
 (0)