From 1022ec3fe4b5c7491eaf271652dd7b64a7355224 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 10 Jun 2010 21:31:39 +0000 Subject: [PATCH] clear python console namespace when used with a new window manager, otherwise old python objects are kept around between opening different blend files (leaking memory). ideally loading a new file would clear the namespace but practically its unliekly to be a problem. --- release/scripts/op/console_python.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/release/scripts/op/console_python.py b/release/scripts/op/console_python.py index 385dd832537..83a3130f80e 100644 --- a/release/scripts/op/console_python.py +++ b/release/scripts/op/console_python.py @@ -33,7 +33,7 @@ def get_console(console_id): ''' helper function for console operators currently each text datablock gets its own - console - bpython_code.InteractiveConsole() + console - code.InteractiveConsole() ...which is stored in this function. console_id can be any hashable type @@ -44,21 +44,24 @@ def get_console(console_id): if consoles is None: consoles = get_console.consoles = {} + else: + # check if clearning the namespace is needed to avoid a memory leak. + # the window manager is normally loaded with new blend files + # so this is a reasonable way to deal with namespace clearing. + # bpy.data hashing is reset by undo so cant be used. + hash_prev = getattr(get_console, "consoles_namespace_hash", 0) + hash_next = hash(bpy.context.manager) - # clear all dead consoles, use text names as IDs - # TODO, find a way to clear IDs - ''' - for console_id in list(consoles.keys()): - if console_id not in bpy.data.texts: - del consoles[id] - ''' + if hash_prev != hash_next: + get_console.consoles_namespace_hash = hash_next + consoles.clear() console_data = consoles.get(console_id) if console_data: console, stdout, stderr = console_data - # XXX, bug in python 3.1.2 ? + # XXX, bug in python 3.1.2 ? (worked in 3.1.1) # seems there is no way to clear StringIO objects for writing, have to make new ones each time. import io stdout = io.StringIO()