Clean up [RESOLVED]

Okay, I’m pretty sure this is working how it’s supposed to but because it’s a matter of garbage collection I’d like to have it confirmed by a second set of eyes.

I have a class that is managing icons floating above character’s heads. Those icons are stored in a list. Here’s how they are generated:

def addPlus(self, color, id):
		self.plus.append([self.assetLoader.modelLoader("/Utilities/Plus.egg"), id])
		self.plus[len(self.plus) - 1][0].setColor(color.getX(), color.getY(), color.getZ())
		self.plus[len(self.plus) - 1][0].reparentTo(self.iconNode)
		self.plus[len(self.plus) - 1][0].setScale(.1,.1,.1)
		self.arrangeIcons()

And here is how they are removed:

def	removePlus(self, id):
		count = 0
		for I in self.plus:
			if(I[1] == id):
				I[0].removeNode()
				self.plus[count:count+1] = []
			else:
				count += 1

Nothing else touches the icon models stored in the list at all. The icons do vanish from the scene, and I know they get removed from the list because I printed it out. That should mean there are no references to them and they get cleaned up, right?

Seems reasonable. Note that:

del self.plus[count]

is equivalent to:

self.plus[count:count+1] = []

and perhaps a little more readable.

You can also use Python’s gc module to detect leaks, or at least things that have been circularly referenced and therefore not cleaned up. You do this with:

import gc
gc.collect()
print gc.garbage

If there’s anything in the gc.garbage list, these are objects that could not be cleaned up because of circular reference-counting issues.

Anything else, if you no longer have a pointer to it, you can assume it’s gone.

David

Thanks for the tips, David. I like that del line much better than the one I’m using.

I started using setPythonTag on the icons instead of pairing them with ID numbers. Do I need to clear those tags for GC to kill the icons?

It depends on whether the thing you add as the python tag contains a reference, directly or indirectly, back to the NodePath itself. If it’s not related to the NodePath in any way, you’re good.

David

Okay, thanks again.