[vtkusers] Help with crashes in VTK

Xiaofeng Z xf10036 at hotmail.com
Wed Sep 7 12:45:14 EDT 2011


>I've been using vtkImageData::New(), followed by vtkImageData->DeepCopy(filter->GetOutput()).  I've been doing this cause I notice that once I delete the filter the output no longer exists.
 Don't deep copy the image, instead do:     vtkImageData* filterOutput = filter->GetOutput();    filterOutput->Register(0);    // increment the ref count to keep the output in the memory. Just make sure you pair off every New() or Register() with a Delete(). BTW, I'm not for or against the use of smart pointer for any fundamental reason.  If one knows very well how a smart pointer class works, using the smart pointer to left the burden of tracking memory allocation can be very helpful.  When I do decided to use smart pointer, I use it exclusively, much like in Java where every variable is a managed smart pointer.  Mixing the usage of smart pointers and naked pointers is where trouble comes in. HTH
On Tue, Sep 6, 2011 at 11:55 AM, David Gobbi <david.gobbi at gmail.com> wrote:

On Tue, Sep 6, 2011 at 12:34 PM, Jonathan Morra <jonmorra at gmail.com> wrote:



This could make sense, I'm going to try and change all my method signatures to pass around const references to vtkSmartPointers where appropriate.  Do you know what happens to the reference count upon returning a vtkSmartPointer?




In VTK, smart pointers should only be used to store references, not to pass them.  VTK methods should take regular pointers as arguments.
The general rules are:



1) Refcounts are not automatically incremented when objects are returned, i.e. when an object is returned to you, then you are just "borrowing" it.  If you need to own it for a while, then store it in a smart pointer.



2) Refcounts are not automatically incremented when objects are passed to a method.  If a method needs to own the object for a while, it should store it in a smart pointer.  All methods in the VTK libraries that need to own the objects that are passed to them will increment the refcount of those objects, either by storing it in a smart pointer or by calling Register() on it.



There are a few very rare exceptions to rule #1, such as the NewInstance() method.
 - David

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110907/7188d766/attachment.htm>


More information about the vtkusers mailing list