[vtkusers] When to delete objects?

Andreas Keil andreas.keil at cs.tum.edu
Thu May 24 04:26:22 EDT 2007


Yep, I also prefer using vtkSmartPointer and think it's a pity that it's
not easier accessible as in ITK. I never found a reason not to use
vtkSmartPointers everywhere, except the additional typing needed.

Andreas


________________________________

From: vtkusers-bounces+andreas.keil=cs.tum.edu at vtk.org
[mailto:vtkusers-bounces+andreas.keil=cs.tum.edu at vtk.org] On Behalf Of
Mike Jackson
Sent: Monday, May 21, 2007 15:54
To: vtkusers at vtk.org
Subject: Re: [vtkusers] When to delete objects?


Also you can use the vtkSmartPointer class to wrap your local objects, so
taking some of the examples from below: 

void myfunction()
{
vtkSmartPointer<vtkSomething> something =
vtkSmartPointer<vtkSomething>::New(); //ref count starts as 1
//do some things here
// 'something' will be deleted when this method returns, saving a line of
code and ensuring that the object is cleaned up.
} 


Just my two cents..

-- 
Mike Jackson Senior Research Engineer
Innovative Management & Technology Services


On May 21, 2007, at 9:40 AM, David E DeMarle wrote:


	Hello,
	
	This is what I do:
	1) Make sure there is a Delete() for each New() around each
temporary (local) use.
	
	void myfunction()
	{
	vtkSomething *something = vtkSomething::New(); //ref count starts
as 1
	//do some things here
	something->Delete(); //ref count -- = 0 which will call
something's destructor to and deallocate it
	}
	
	2) When I want to keep the object around and use it later, I save
it in an ivar within a class, and destroy it in the classes destructor, or
whenever the instance of the object is replaced with another one.
	
	class MyClass { 
	MyClass()
	{
	this->MySomething = NULL;
	} 
	~MyClass()
	{
	if (this->MySomething != NULL)
	{
	this->MySomething->Delete();
	}
	}
	
	void MyFunction() 
	{
	if (this->MySomething != NULL) //Delete what I might have made
before
	{
	this->MySomething->Delete();
	}
	this->MySomething = vtkSomething::New();//Create a new thing it
starts with ref count 1 
	
	//now you can use it and not call Delete(), because the destructor
will eventually
	for (int i = 0; i < 99; i++)
	{
	this->MySomething->CallAMethod();
	...
	this->UseIt(); 
	...
	}
	}
	
	void UseIt()
	{
	if (this->MySomething) //sanity check
	{
	this->MySomething->DoSomething();
	}
	}
	}
	
	3) If I need to pass a reference to another object I register it.
	MyClass::PassItOff(AnotherClass *other)
	{
	if (this->MySomething)
	{
	other->HisCopy == this->MySomething();
	this->MySomething->Register(); //ref count goes to 2, so now it
will only be freed when this, and other Delete() it to make ref count 0
	}
	}
	
	Note:
	For subclasses of vtkObject you don't have to go through the steps
of checking the old pointer, saving the new pointer and incrementing the
reference count manually, you can just use the vtkSetObject Macro in
VTK/Common/vtkSetGet.h 
	So 
	
	Also note: to test if you've got it right, turn on the
VTK_DEBUG_LEAKS flag in cmake. When on, the program will check for any
vtkObjects that have not been Delete()'d thoroughly as it ends and print
them. 
	
	hth,
	Dave DeMarle
	
	
	
	
	On 5/21/07, burlen <burlen at apollo.sr.unh.edu> wrote: 

		Hi,
		Here is what I do, 
		
		1) when ever you ::New() something you also Delete() it.
		
		2a) When ever you need to keep the output of a filter
longer than the filter itself call Register(0) on the output then Delete()
the filter. Make sure you Delete() the output later.
		
		2b) alternatively you could DeepCopy the output of filters
you want to keep around.
		
		Burlen
		
		
		Godofredo wrote: 

			Hi everyone. I'm trying to make the triangulation
of a point cloud using
			vtkDelaunay2D. What I don't know is when to
exactly delete filters and other
			objects such as PolyDatas that aren't used
anymore. I've made some tries but
			
			with bad results. For example, if I use
vtkDelaunay2D, update it and then
			save its output in a vtkPolyData, can I delete the
vtkDelaunay2D filter?
			Many thanks. 
			  


		-- 
		
		
		_______________________________________________
		This is the private VTK discussion list.
		Please keep messages on-topic. Check the FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ
		Follow this link to subscribe/unsubscribe:
		http://www.vtk.org/mailman/listinfo/vtkusers 
		
		
		


	_______________________________________________
	This is the private VTK discussion list. 
	Please keep messages on-topic. Check the FAQ at:
http://www.vtk.org/Wiki/VTK_FAQ
	Follow this link to subscribe/unsubscribe:
	http://www.vtk.org/mailman/listinfo/vtkusers






More information about the vtkusers mailing list