<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><div>If I can solve one or two more problems here, I'll be looking at replacing an existing system of around 12,000 lines of OpenGL code with a couple of hundred lines of VTK code, and the VTK graphics look better. It's probably a common situation, the data involved is fairly simple but the existing code for presenting it is byzantine and borderline unmaintainable. <br><br><span>But there's a big and non-obvious problem with a memory leak in the picture. The application reads data from a proprietary file, writes that data out to a .vtk file, and then calls a function which uses VTK code to draw the data on a VTK form which is created using the sourceforge wrapper library and component for c# available at <a target="_blank"
href="http://vtkdotnet.sourceforge.net/">http://vtkdotnet.sourceforge.net/</a>.</span><br><br>The problem I'm seeing is definitely in the VTK code and not any part of the code which generates the data. If I insert a return statement just prior to the call to the vtk code, the memory leak vanishes.<br><br>Worse, the Red-Gate ANTS memory profiler offers no help; it shows the largest objects in the system being a couple of inconsequential byte arrays which are definitely not part of the problem.<br><br>The examples provided show functions with names like AddConeSurfaceToWindow and AddFlamingoToWindow.<br><br>Trying to use the same techniques produces a function which looks like this:<br><br><div style="margin-left: 80px; color: rgb(0, 128, 255);"><font size="1"> // 3d texture<br> void AddBagVolumeToWindow4(vtk.vtkRenderWindow
renWin)<br> {<br> // 3d texture map<br> vtkStructuredPointsReader reader = new vtkStructuredPointsReader();<br> vtkVolume volume = new vtkVolume();<br> vtkVolumeProperty volumeProperty = new vtkVolumeProperty(); <br> vtkRenderer renderer = new vtkRenderer();<br> vtkRenderWindowInteractor interactor = new vtkRenderWindowInteractor();<br> vtkVolumeTextureMapper3D volumeMapper = new
vtkVolumeTextureMapper3D();<br> vtkPiecewiseFunction opacity = new vtkPiecewiseFunction();<br> vtkColorTransferFunction color = new vtkColorTransferFunction();<br><br> reader.SetFileName("c:\\zz.vtk");<br> reader.Update();<br> ifileLoaded = 1; <br><br> opacity.AddPoint(0, 0);<br> opacity.AddPoint(64, .005);<br> opacity.AddPoint(127, .01);<br> opacity.AddPoint(128,
.05);<br> opacity.AddPoint(200, .5);<br><br> color.AddRGBPoint(0, 1, 1, 1);<br> color.AddRGBPoint(126, .665, .84, .88);<br> color.AddRGBPoint(127, 0, 1, 0);<br> color.AddRGBPoint(199, 1, 0, 0);<br><br> volumeProperty.SetColor(color);<br> volumeProperty.SetScalarOpacity(opacity);<br> volumeProperty.SetInterpolationTypeToLinear();<br>
volumeProperty.ShadeOn();<br><br> volumeMapper.SetInput(reader.GetOutput());<br><br> volume.SetMapper(volumeMapper);<br> volume.SetProperty(volumeProperty);<br><br> renderer.SetBackground(1, 1, 1);<br> renderer.AddVolume(volume);<br> interactor.SetRenderWindow(renWin); <br><br> renWin.AddRenderer(renderer);<br><br>
renWin.Render();<br> interactor.Start();<br><br> reader.Dispose();<br> volume.Dispose();<br> volumeProperty.Dispose();<br> renderer.Dispose();<br> interactor.Dispose();<br> opacity.Dispose();<br> color.Dispose();<br><br> vtk.vtkWin32OpenGLRenderWindow win32win =<br>
vtk.vtkWin32OpenGLRenderWindow.SafeDownCast(renWin);<br> win32win.Clean();<br> }<br></font></div><br>Again testing indicates that the memory leak has nothing to do with code outside of this function and a memory profiler reputed to be the best provides no information. I get the impression that many if not most VTK apps only need to put a particular 3D image on a screen once, while this function is called repeatedly as a succession of images is read in by the user. Again the read process reads data and throws it out to c:\\zz.vtk, which the vtk code then reads.<br><br>It occurred to me that eliminating the repeated VTK constructor calls might help. That leads to this code in the app constructor:<br><br><div style="margin-left: 80px; color: rgb(0, 128, 255);"><font size="1"> public
Form1()<br> { <br> InitializeComponent();<br><br> // clear out vtk widget screen<br> vtkForm1.GetRenderWindow().Frame();<br> vtkRenderer vr = new vtkRenderer();<br> vr.SetBackground(1, 1, 1);<br> vr.SetRenderWindow(vtkForm1.GetRenderWindow());<br> vtkForm1.GetRenderWindow().AddRenderer(vr);<br> vr.Clear();<br>
vr.Render();<br> vtk.vtkWin32OpenGLRenderWindow win32win =<br> vtk.vtkWin32OpenGLRenderWindow.SafeDownCast(vtkForm1.GetRenderWindow());<br> win32win.Clean();<br><br> // set viewing objects for 3D texture map model <br> volume4 = new vtkVolume();<br> volumeProperty4 = new vtkVolumeProperty();<br> renderer4 = new vtkRenderer();<br> interactor4 = new
vtkRenderWindowInteractor();<br> volumeMapper4 = new vtkVolumeTextureMapper3D(); <br><br> opacity = new vtkPiecewiseFunction();<br> color = new vtkColorTransferFunction();<br> <br> renderer4.AddVolume(volume4); <br> vtkForm1.GetRenderWindow().AddRenderer(renderer4);<br> volume4.SetMapper(volumeMapper4);<br>
volume4.SetProperty(volumeProperty4); <br> }<br></font></div><br>and then a modified function to display images:<br><br><div style="margin-left: 80px; color: rgb(0, 128, 255);"><font size="1"> void AddBagVolumeToWindow4(vtk.vtkRenderWindow renWin)<br> {<br> <br> vtkStructuredPointsReader reader4 = new vtkStructuredPointsReader();<br> reader4.SetFileName("c:\\zz.vtk");<br> reader4.Update();<br><br> opacity.AddPoint(0, 0);<br>
opacity.AddPoint(64, .005);<br> opacity.AddPoint(128, .01);<br> opacity.AddPoint(200, .05);<br> opacity.AddPoint(220, .4);<br><br> color.AddRGBPoint(64, .665, .84, .88);<br> color.AddRGBPoint(128, 0, .746, .95);<br> color.AddRGBPoint(220, 0, 1, 0);<br> color.AddRGBPoint(230, 1, 0, 0);<br><br> volumeProperty4.SetColor(color);<br>
volumeProperty4.SetScalarOpacity(opacity);<br> volumeProperty4.SetInterpolationTypeToLinear();<br> volumeProperty4.ShadeOn();<br><br> volumeMapper4.SetInput(reader4.GetOutput());<br> volumeMapper4.Update();<br><br> renderer4.SetBackground(1, 1, 1); <br> interactor4.SetRenderWindow(renWin); <br> <br>
renWin.Render();<br> interactor4.Start();<br><br> reader4.Dispose();<br> GC.Collect();<br> <br> vtk.vtkWin32OpenGLRenderWindow win32win =<br> vtk.vtkWin32OpenGLRenderWindow.SafeDownCast(renWin);<br> win32win.Clean(); <br> <br> }<br></font></div><br>But even that doesn't help. The computer loses around 50MB each time a new image is read in and these
images are not that large, more like around 10MB on average. <br><br>Again I'm new at this, and any help or information would be appreciated.<br><br><br>Ted<br><br><br><br><br><br><br><br><br><br><br><br><br><br></div></div><br>
</body></html>