[vtkusers] When to delete objects?

Mike Jackson imikejackson at gmail.com
Thu May 24 07:21:32 EDT 2007


If you are doing a

myPolyDataOutput* output = cleanPolyFilter->GetOutput();
return output;

where cleanPolyDataFilter is a smartPointer then when the function  
returns the variable 'output' will become invalid because  
'cleanPolyDataFilter' goes out of scope and is deleted. The way to do  
this is something like:

myPolyDataOutput* output = cleanPolyFilter->GetOutput();
vtkPolyData* decoupledPolyData = vtkPolyData::New();
decoupledPolyData->shallowCopy( output );
return decoupledPolyData;

This should allow you to return a valid pointer.

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


On May 24, 2007, at 5:39 AM, Godofredo wrote:

>
> Well, again many many thanks. I followed Mike's suggestion and used
> vtkSmartPointer for my local objects. It worked ok for all local  
> objects but
> for the last filter I use in the local function. I explain the code  
> flow so
> you can make an idea:
> I have a function that takes a point cloud stored in a PolyData as  
> input and
> extracts a set of points which are inside a predefined volume. The  
> output of
> this function is also a PolyData. The flow is: define 2 implicit  
> spheres
> (vtkSphere) -> calculate the volume difference (vtkImplicitBoolean)
> ->extract the points inside the calculated volume
> (vtkExtractPolyDataGeometry)->clean the data (vtkCleanPolyData) ->  
> store the
> output of vtkCleanPolyData in a Polydata which the function  
> returns. Next to
> this I call another function which triangulates the extracted points.
> The problem is that I can  wrap all objects using vtkSmartPointer  
> except the
> last filter (i.e. vtkCleanPolyData). If I wrap that one the  
> execution of the
> code crashes when calling the delaunay filter in the next function.  
> Any
> ideas?
>
>
>
> Mike Jackson-9 wrote:
>>
>> 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
>>
>>
>> _______________________________________________
>> 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
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/When-to-delete- 
> objects--tf3789224.html#a10780447
> Sent from the VTK - Users mailing list archive at Nabble.com.
>
> _______________________________________________
> 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