[vtkusers] Memory leak with wxVTKRenderWindow? Please help.

Marcus Stojek stojek at part-gmbh.de
Tue May 6 08:35:38 EDT 2003


Hi,

I am using vtk40 with Python 2.2 and wxPython on Windows NT.
The software I am working on is a kind of viewer for simulation
results. When opening and closing one result files and viewing
it with vtk repeatedly, the memory usage increases with each new widget I
create. The memory isn't returned to the system when the widget is
destroyed. Even for small modells with only 10 or 15 triangles I am
loosing app 5M of memory with each widget.

The example code below should demonstrate the problem. Either click
'Generate Data' once and then 'Render Data' and 'CloseRenderWindow'
alternately for several times. Or just create a widget without data
and renderer and then close it for several times ('MakeWidget' and
'CloseRenderWindow').

Watch the output or your Taskmanager for memory usage.

Is there something wrong with the wxVTKRenderWindow?
Or with my code?

Thanks,
Marcus

#-----------------------------snip------------------------------
from Numeric import array
from wxPython.wx import *
from vtkpython import *
import wxVTKRenderWindow
import random
import calldll,struct

#---------------------------------------------------------------------------
--------------------------
class MainFrame(wxFrame):
    def __init__(self):
        wxFrame.__init__(self, parent=NULL ,id= -1, title='VTK Viewer', size
= (500, 600))

        ID_GEN=wxNewId()
        ID_SHOWCALC=wxNewId()
        ID_DELDAT=wxNewId()
        ID_CLOSE=wxNewId()
        ID_WIDG=wxNewId()

        testmenu=wxMenu()
        testmenu.Append(ID_GEN,     'Generate Data')
        testmenu.Append(ID_WIDG,    'Make Widget')
        testmenu.Append(ID_SHOWCALC,'Render Data')
        testmenu.Append(ID_DELDAT,  'Delete Data')
        testmenu.Append(ID_CLOSE,   'Close RenderWindow')

        EVT_MENU(self,ID_GEN,self.DoGenerate)
        EVT_MENU(self,ID_SHOWCALC,self.DoRenderExample)
        EVT_MENU(self,ID_DELDAT,self.DoDeldat)
        EVT_MENU(self,ID_CLOSE,self.DoClose)
        EVT_MENU(self,ID_WIDG,self.DoMakeWidget)

        self.menubar = wxMenuBar()
        self.menubar.Append(testmenu,'Test')
        self.SetMenuBar(self.menubar)

        #Number of triangles
        self.trinum = 15

        self.dataok   = 0
        self.memory   = 0
        self.getmeminfo()
#------------------------------------------------------
    def DoGenerate(self,event):
        print 'before DoGenerate',
        self.getmeminfo()
        self.nodenum = self.trinum  * 3
        self.koornum = self.nodenum * 3

        g = random.Random()
        pointlist=[]
        i=0
        while i < self.koornum:
            pointlist.append(10.0*random.random())
            i=i+1

        celltypelist   = self.trinum *[5]

        trilist = []
        i=0
        while i < self.trinum:
            n=3*i
            trilist.extend([3,n,n+1,n+2])
            i=i+1

        loclist = range(0,self.trinum*4,4)

        self.p         = array(pointlist,'f')
        self.celltype  = array(celltypelist,'c')
        self.triarray  = array(trilist,'i')
        self.locarray  = array(loclist,'i')

        self.dataok=1
        print 'after DoGenerate ',
        self.getmeminfo()
#------------------------------------------------------
    def DoDeldat(self,event):
        print 'before DelDat',
        self.getmeminfo()
        del(self.p)
        del(self.celltype)
        del(self.triarray)
        del(self.locarray)
        self.dataok = 0
        print 'after DelDat',
        self.getmeminfo()
#------------------------------------------------------------
    def getmeminfo(self):
        result   = {}
        kernel32 = calldll.load_library('kernel32')
        memstat  = calldll.get_proc_address(kernel32,'GlobalMemoryStatus')
        if memstat:
            buf = calldll.membuf(32)
            buf.write(struct.pack('1L',32))
            calldll.call_foreign_function(memstat,'l','',(buf.address(),))
            rtuple = struct.unpack('8L',buf.read())
            result['MemoryLoad']    = rtuple[1]/1024.0   # [0] is len
            result['TotalPhys']     = rtuple[2]/1024.0
            result['AvailPhys']     = rtuple[3]/1024.0
            result['TotalPageFile'] = rtuple[4]/1024.0
            result['AvailPageFile'] = rtuple[5]/1024.0
            result['TotalVirtual']  = rtuple[6]/1024.0
            result['AvailVirtual']  = rtuple[7]/1024.0
        a=result['TotalPhys']-result['AvailPhys']
        b=result['TotalVirtual']-result['AvailVirtual']
        if self.memory==0:
            self.memory=a+b
            print '                      AvailPhys AvailVirtual'
            print '                  %10.f
%10.f'%(result['AvailPhys'],result['AvailVirtual'])
        else:
            print '%10.f %10.f'%(result['AvailPhys'],result['AvailVirtual'])
            #print '%10.f'%((a+b))#-self.memory)
#------------------------------------------------------
    def DoRenderExample(self,event):
        if self.dataok:
            print 'before Render    ',
            self.getmeminfo()

            self.vpoints   = vtkFloatArray()
            self.vpoints.SetNumberOfComponents(3)
            self.vpoints.SetVoidArray(self.p,self.koornum,1)
            self.points = vtkPoints()
            self.points.SetData(self.vpoints)

            self.cidTArray = vtkUnsignedCharArray()
            self.cidTArray.SetVoidArray(self.celltype,self.trinum,1)

            self.IdTArray=vtkIdTypeArray()
            self.IdTArray.SetVoidArray(self.triarray,4*self.trinum,1)

            self.vtkCArray=vtkCellArray()
            self.vtkCArray.SetCells(self.trinum,self.IdTArray)

            self.vtkLocArray=vtkIntArray()
            self.vtkLocArray.SetVoidArray(self.locarray,self.trinum,1)

            self.grid=vtkUnstructuredGrid()

self.grid.SetCells(self.cidTArray,self.vtkLocArray,self.vtkCArray)
            self.grid.SetPoints(self.points)

            self.MapperSurf=vtkDataSetMapper()
            self.MapperSurf.SetInput(self.grid)
            self.MapperSurf.ScalarVisibilityOff()

            self.ActorSurf=vtkActor()
            self.ActorSurf.SetMapper(self.MapperSurf)
            self.PropSurf=vtkProperty()
            self.PropSurf.SetRepresentationToSurface()
            self.ActorSurf.SetProperty(self.PropSurf)

            self.ren = vtkRenderer()
            self.ren.AddActor(self.ActorSurf)

            self.gfilter=vtkGeometryFilter()
            self.gfilter.SetInput(self.grid)

            self.edges=vtkExtractEdges()
            self.edges.SetInput(self.gfilter.GetOutput())

            self.tubes = vtkTubeFilter()
            self.tubes.SetInput(self.edges.GetOutput())
            self.tubes.SetRadius(0.15)
            self.tubes.SetNumberOfSides(6)

            self.edgemap = vtkPolyDataMapper()
            self.edgemap.SetInput(self.tubes.GetOutput())
            self.edgeActor = vtkActor()
            self.edgeActor.GetProperty().SetColor(1,0,0)
            self.edgeActor.SetMapper(self.edgemap)
            self.ren.AddActor(self.edgeActor)

            self.widget =
wxVTKRenderWindow.wxVTKRenderWindow(self, -1,stereo=1)
            self.widget.GetRenderWindow().AddRenderer(self.ren)

            self.widget.Render()
            self.widget.Show(true)
            #self.SendSizeEvent() #depends on version of wxVTKRenderWindow
            print 'after Render     ',
            self.getmeminfo()
#------------------------------------------------------
    def DoMakeWidget(self,event):
        print 'before MakeWidget',
        self.getmeminfo()

        self.widget = wxVTKRenderWindow.wxVTKRenderWindow(self, -1,stereo=1)

        print 'after MakeWidget ',
        self.getmeminfo()
        self.datok = 0
#------------------------------------------------------
    def DoClose(self,event):
        print 'before DoClose   ',
        self.getmeminfo()
        if self.dataok:
            del(self.points)
            del(self.vpoints)
            del(self.cidTArray)
            del(self.IdTArray)
            del(self.vtkCArray)
            del(self.vtkLocArray)
            del(self.grid)
            del(self.MapperSurf)
            del(self.ActorSurf)
            del(self.PropSurf)
            del(self.gfilter)
            del(self.edges)
            del(self.tubes)
            del(self.edgemap)
            del(self.edgeActor)
            del(self.ren)
        self.widget.Destroy()
        print 'after DoClose    ',
        self.getmeminfo()
        pass
#----------------------------------------------------------------------
class MyApp(wxApp):
    def OnInit(self):
        self.frame = MainFrame()
        self.frame.Show(true)
        self.SetTopWindow(self.frame)
        return true
#-----------------------------------------------------------------
app = MyApp(0)
app.MainLoop()
sys.exit()
#-------snap---------------------------------------




More information about the vtkusers mailing list