[vtkusers] deallocating objects within functions

Mike Jackson imikejackson at gmail.com
Tue Apr 22 21:14:33 EDT 2008


Look at vtkSmartPointer.h. Basically you use one of these to wrap  
your object and when it goes out of scope your object will get  
deleted for you.

vtkStructuredPoints* GetField (int index)
{
vtkSmartPointer< vtkImageExtractComponents > extr = vtkSmartPointer<  
vtkImageExtractComponents >::New();
     extr->SetInput (this->GetInputField ());
     extr->SetComponents (index);
     extr->Update ();
vtkStructuredPoints* pts = xtr->GetOutput ();
pts->Register(NULL)
     return pts;
}

Note, that pts will have a ref count of 1 by the time it returns to  
the calling function. When the calling function is done with "pts",  
it should call pts->Delete();.

Hope that helps.
-- 
Mike Jackson   Senior Research Engineer
Innovative Management & Technology Services


On Apr 22, 2008, at 6:32 PM, Adrian Albert wrote:

> Dear All,
>
> I have a small problem in vtk: I have a class (not derived from any  
> VTK object) that has methods for dealing with vtkImageData in  
> various ways. For example, I am extracting one scalar component  
> from a multi-component vtkImageData object using this function
>
> vtkStructuredPoints* GetField (int index)
> {
>     vtkImageExtractComponents *extr =  
> vtkImageExtractComponents::New ();
>     extr->SetInput (this->GetInputField ());
>     extr->SetComponents (index);
>     extr->Update ();
>     return (vtkStructuredPoints*) (extr->GetOutput ());
> }
>
> I am wondering if there is a problem with deallocating the extr  
> object in the above function. I obviously cannot manually call  
> Delete () on it, since I must use it for returning my output. Is  
> the Delete () method automatically called when returning from the  
> function to the calling routine? How 'big' is the extr object? Is  
> it of the same size as the input to the vtkImageExtractComponents  
> filter?
>
> Thanks for taking the pain in reading this!
> Adrian
> -- 
> ---------------------------------------------------
> Adrian Albert
> School of Engineering and Science
> Jacobs University Bremen
> Germany
> Tel.: 00494212003258
> _______________________________________________
> 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