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.
This commit is contained in:
Campbell Barton 2010-06-10 21:31:39 +00:00
parent 6aaf55eee5
commit 1022ec3fe4

@ -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()