[vtkusers] vtkRenderer.RemoveActor() not removing actor?

Phil Cook p.cook at cs.ucl.ac.uk
Fri Nov 28 07:01:30 EST 2003


I have a problem where I can't release the memory used by my actors 
after calling vtkRenderer.RemoveActor(vtkActor). As far as I can see, 
there aren't any other references to the actor around.

I modified SimpleVTK.java to illustrate the problem. I am using Java on 
Linux, with yesterday's CVS VTK. When running this program and watching 
top, I see the memory footprint grow whenever I press the button. It 
will grow far beyond the maximum Java stack size, so I think the 
problem is in the native code. Is there a safe way to delete the actor?

Thanks,


Phil


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import vtk.*;


public class TestMemoryLeak extends JPanel implements ActionListener {

     static JFrame frame;
     vtkPanel renWin;
     JButton addRemoveActors;

     // We will remove add this to the renderer, then remove it 
repeatedly
     vtkActor coneActor = new vtkActor();

     static {
         System.loadLibrary("vtkCommonJava");
         System.loadLibrary("vtkRenderingJava");
     }



   public TestMemoryLeak() {
     setLayout(new BorderLayout());

     renWin = new vtkPanel();
     vtkConeSource cone = new vtkConeSource();
     cone.SetResolution(60);
     vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
     coneMapper.SetInput(cone.GetOutput());

     coneActor.SetMapper(coneMapper);

     renWin.GetRenderer().AddActor(coneActor);

     addRemoveActors = new JButton("Add more actors");
     addRemoveActors.addActionListener(this);

     add(renWin, BorderLayout.CENTER);
     add(addRemoveActors, BorderLayout.SOUTH);
   }


     public void actionPerformed(ActionEvent e)
     {
         if (e.getSource().equals(addRemoveActors))
             {

                 for (int i = 0; i < 1000; i++) {

                     renWin.GetRenderer().RemoveActor(coneActor);

                     vtkConeSource cone = new vtkConeSource();
                     cone.SetResolution(60);
                     vtkPolyDataMapper coneMapper = new 
vtkPolyDataMapper();
                     coneMapper.SetInput(cone.GetOutput());

                     // this overwrites the reference to coneActor,
                     // so shouldn't it be garbage collected?
		 // if you call coneActor.GetReferenceCount() here, it returns 1
                     coneActor = new vtkActor();

                     coneActor.SetMapper(coneMapper);

                     renWin.GetRenderer().AddActor(coneActor);
                 }
             }
     }

   public static void main(String s[])
   {
     TestMemoryLeak panel = new TestMemoryLeak();

     frame = new JFrame("TestMemoryLeak");
     frame.addWindowListener(new WindowAdapter()
       {
         public void windowClosing(WindowEvent e) {System.exit(0);}
       });
     frame.getContentPane().add("Center", panel);
     frame.pack();
     frame.setVisible(true);
   }
}




More information about the vtkusers mailing list