[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29394] trunk/blender/release/scripts/op/ console_python.py: 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).

Campbell Barton ideasman42 at gmail.com
Thu Jun 10 23:31:44 CEST 2010


Revision: 29394
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29394
Author:   campbellbarton
Date:     2010-06-10 23:31:39 +0200 (Thu, 10 Jun 2010)

Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/release/scripts/op/console_python.py

Modified: trunk/blender/release/scripts/op/console_python.py
===================================================================
--- trunk/blender/release/scripts/op/console_python.py	2010-06-10 21:23:09 UTC (rev 29393)
+++ trunk/blender/release/scripts/op/console_python.py	2010-06-10 21:31:39 UTC (rev 29394)
@@ -33,7 +33,7 @@
     '''
     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 @@
 
     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()





More information about the Bf-blender-cvs mailing list