[vtkusers] vtk smart pointer

Shawn Waldon shawn.waldon at kitware.com
Mon Nov 27 11:17:18 EST 2017


Hi,

So I don't have time right now to write the answer this needs, but I'll
give you something to experiment with:

The base class vtkObject that contains the reference count used by
vtkSmartPointer/vtkNew has a method you can call to see the reference
count: GetReferenceCount.  It is not recommended for applications to use
this directly, but you can use it to experiment to see what happens to the
reference count of an object at different points in your program.

As far as using vtkSmartPointer/vtkNew in functions here are some general
tips:

If the object is a parameter of the function, then whoever called the
function should guarantee that the object stays valid for the lifetime of
the function call.
If you are creating the object in a function and want it to be deleted when
the function exits use vtkSmartPointer/vtkNew to manage that (don't call
Delete yourself--it is too easy to forget)
If you are returning a vtkObject from your function, you can change the
return type to vtkSmartPointer (vtkNew doesn't work here)

For more see [1].

HTH,
Shawn

[1]: https://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers

On Fri, Nov 24, 2017 at 5:35 AM, ran <kor1581 at gmail.com> wrote:

> I have few doubts about vtk smart pointers usage, as in case of different
> scenarios explained below
>
> Scenario1:
>
> For example if I have two class Red and Blue with following structure
>
> Class Red
> {
> Public:
>         Red(){}
> SetVTKtem(vtkSmartPointer<vtkType>  ptrParam)
> {
>         ptr  =  ptrParam
> }
> private:
>         vtkSmartPointer<vtkType> ptr  ;
> };
>
> Class Blue
> {
> Public:
>         Blue ()
>         {
>                 ptr1   = vtkSmartPointer< vtkType >::New();
> ptr2   = vtkSmartPointer< vtkType >::New();
>         }
>         vtkSmartPointer<vtkType> GetPtr1()
> {
>         return ptr1 ;
> }
>         vtkSmartPointer<vtkType> GetPtr2()
> {
>         return ptr2 ;
> }
>   private:
>         vtkSmartPointer<vtkType> ptr1  ;
> vtkSmartPointer<vtkType> ptr2  ;
> };
>
> Then in main method,
>
> void main()
> {
> Blue *objBlue = new Blue ;
> Red * objRed =  new Red;
>
> //In a series of events in main, will set prt1, ptr2, ptr3 in objBlue to
> ptr
> in objRed
>
> //in first event, set
>
> objRed ->SetVTKtem(objBlue -> GetPtr1());
>
> // second event, set
> objRed ->SetVTKtem(objBlue -> GetPtr2());
>
>
> // in third event, again will set
> objRed ->SetVTKtem(objBlue -> GetPtr1());
>
>
> //Then if  delete objBlue, what happens to the object vtk objects pointed
> by ptr1, ptr2, ptr3.
>
> delete objBlue;
> }
> Then
> 1)      After calling,
> objRed ->SetVTKtem(objBlue -> GetPtr2());
> will reference of ptr1 of objBlue decrement to one?
> will reference of ptr of objRed decrement to one?
>
> 2)      After deleting objBlue, what happens to the object vtk objects
> pointed
> by ptr1, ptr2, ptr3?
> Will there reference be zero?
>
>
> Scenario2:
>
> I have another doubt,
>  is it safe to call vtkSmartPointer and vtkNew pointers in local variable
> to
> functions which calling recursively?
> For example
>
> Class Yellow
> {
> public:
> function()
> {
>                 vtkNew<vtkType1> ptrType1  = vtkSmartPointer<
> vtkType1>::New();
> }
> };
>
>
> Then in main method,
> void main()
> {
> Yellow objYellow = new Yellow;
> //Then in a series of events
>
> //In first event
>         objYellow-> function();
> //In second event
> objYellow-> function();
> ..
> }
>
> After second call to function(), will object pointed by ptrType1  in first
> call be function be deleted?
>
> Scenario3:
> Another doubt is if,
>
> Class Red
> {
> Public:
>         Red(){}
> SetVTKtem(vtkSmartPointer<vtkType>  ptrParam)
> {
>         ptr  =  ptrParam
> }
> private:
>         vtkSmartPointer<vtkType> ptr  ;
> };
>
> Class Blue
> {
> Public:
>         vtkSmartPointer<vtkType> GetPtr1()
> {
>         return ptr1.GetPointer() ;
> }
>   private:
>         vtkNew<vtkType> ptr1  ;
> };
>
>
> Then in main function,
>
> void main()
> {
> Blue *objBlue = new Blue ;
> Red * objRed =  new Red;
>
> //In a series of events in main, will set prt1, ptr2, ptr3 in objBlue to
> ptr
> in objRed
>
> //in first event, set
>
>
>
>
>
> --
> Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171127/025906af/attachment.html>


More information about the vtkusers mailing list