Possible memory leak?

running the below text snippet on Windows and OS X reveals a possible memory leak?

test 1

  • start the program
  • repeatedly press ‘n’ to create a card & textur (be aware that the previous node should be deleted correctly and because their is no further reference to the texture, it should be gone too)
  • observe the memory with the task manager

test 2

  • remove or comment-out the line #32
self.np.setTexture(cardTexture)
  • do test 1

result test 1
memory jumps up as we press ‘n’ more and more

result test 2
memory stays stable

is this a memory leak or am i doing something terribly wrong?

this was tested on Panda3D 1.2.3 Windows and my self-build OS X version. same behaviour on both platforms.

thanks,
kaweh

import sys

import direct.directbase.DirectStart

from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import PNMImage, CardMaker, Texture

class TextureMemoryTest(DirectObject):
    def __init__(self):
        self.np = None
        self.accept('n', self.newTexture)
        self.accept('escape', sys.exit)
        
    def newTexture(self):
        print 'newTexture'
        
        if self.np:
            self.np.removeNode()
            self.np = None
        
        cardMaker = CardMaker('test')
        cardMaker.setFrame(-10, 10, -10, 10)
        cardMaker.setHasUvs(True)

        texture = PNMImage(256, 256) 
        cardTexture = Texture()
        cardTexture.load(texture)
        print cardTexture
        
        self.np = render.attachNewNode(cardMaker.generate())
        self.np.setP(-90)
        self.np.setTexture(cardTexture)
        
test = TextureMemoryTest()        
run()

Ah, it’s not a leak per se; you’re just filling up a cache. Eventually, the cache will fill up, and it will start to evict old textures from the cache. By default, the cache is quite large, however.

Try setting the following in your Config.prc:

geom-cache-size 0

This will turn off the cache and restore the behavior you might expect.

It is a bit of a design bug that (a) the cache entries don’t automatically evict themselves as you release the Texture pointers, or that (b) there aren’t even very good interface tools for forcing the cache to clean itself when you know the Textures in question have been released. I’ll see if I can improve this situation.

David