Garbage collection


Qustion on python.

I use 3 separate NMesh objects to call three times a subroutine
with different parameters, then I collect all mesh data
in a fourth mesh object.

This fourth is the only for wich PutRaw is invoked.

All this is actually within a subroutine

More or less:

	MeshQ1 = NMesh.GetRaw()
	MeshQ2 = NMesh.GetRaw()
	MeshQ3 = NMesh.GetRaw()

	MeshT = NMesh.GetRaw()
	for v in MeshQ1.verts:
	for f in MeshQ1.faces:
	for v in MeshQ2.verts:
	for f in MeshQ2.faces:
	for v in MeshQ3.verts:
	for f in MeshQ3.faces:


Except that MeshT is actually between the arguments of this subroutine and the PutRaw happens in the caller.

Question is:

Those three meshes MeshQ1,MeshQ2 and MeshQ3 needs to be
explicitely destroyed at the end of the subroutine
to prevent memory leaks?

And if yes, how?

THis subroutine is going to be called 8 to 20 times in a row and meshes can be quite big, and user can call it more than once…

Thanx in advance


-Use the garbage collector module(gc) from python:

import gc
del variable


Unrelated to your question, but this:

  for v in MeshQ1.verts: 
  for f in MeshQ1.faces: 

can be shortened to this:


Thats pretty cool you guys. I’m glad you started this thread S68. I’ve been wondering about garbage collection for a bit.

Blend On!

Thanx, I’ll give both suggestions (del and sortening) a try.

In any case script runs the first time, and crashes Blender on second run. Furthermore newely created objecs are single sided (?) usually with AutoSmooth on and sometimes SubSurfed…

something weird is happening here.


just before you do


set the drawmode of the mesh explicitely to not subsurfaced and twosided with:

MeshT.mode = 260

as jms pointed out overhere:


Aw, thanx for this tip too!

I’l check script on a good machine today, if it still crashes Blender I’ll ask for other helps!


Hehe… was a NT 4.0 bug!

On Win 2k I don’t even have the singleside-Autosmooth issues!

Anyhow I’ll add that line :slight_smile:


Beware! BWF 0.0.8 next to be released!

Blender python objects usually have a pointer to a datablock of info (malloc() allocated), but NMesh is a little different.
Calling getRaw() without a parameter will not link the mesh datablock (malloc() allocated) to the python mesh you created (this is normal behavior). Instead it will create a python representation of the data, and set the python object’s data pointer to NULL.
Calling update() on a python mesh creates a new datablock which represents the python mesh’s values.
Also calling PutRaw() will free the datablock (if any) pointed to and the write/rewrite it using the ‘python mesh’ data.
So in your case you are simply overwriting the python mesh’s data and there have been no calls to malloc() to allocate data. (at least until you call putRaw())

As a slide note heres a little blip about blenders gc:(copies out of datablock.c)
" *Blender Objects have their own ‘reference counting’, e.g. a Mesh datablock used by two Objects has a user count of 2. Datablocks with user count of 0 are not saved to Disk – this is the current way Blender does
‘garbage collection’ Therefore, an object should normally not be deleted by Python, but rather unlinked from its parent object. …"

Setting modes(from the python API):
This might be a better way :). Call myMesh.setMode(). Parameters: “NoVNormalsFlip”, “TwoSided”, “AutoSmooth”, “SubSurf”

Anyway sorry for the overkill. :o