vtkSmartPointer usage question

View: New views
11 Messages — Rating Filter:   Alert me  

vtkSmartPointer usage question

by Alex Malyushytskyy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Assume I have the following pseudo-code ( :

vtkSmartPointer <Class> sptr = vtkSmartPointer <Class>::New();
....................
// case a
Class* p1 = Class::New();

// case b
vtkSmartPointer <Class> sptr2 = vtkSmartPointer <Class>::New();
............................

What is the correct way to associate smart pointer with
a) newly allocated pointer to the Class,
b) pointer another smart pointer points to

so currently associated  data is properly released and memory deallocated?
It looks like simple assigning does not work .

for the case 'a' I thought that
    sptr->Take(p1)
should work, but it seems in may case previously associated data is
not destroyed (destructor is not called), so I assume it is not
dereferenced for some reason.

for the case 'b'  my assumptions was that simple assigning should work:

sptr = sptr2;


Thanks in advance,
            Alex
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by Michael Jackson-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

case b.
--
Mike Jackson <www.bluequartz.net>

On Nov 4, 2009, at 8:17 PM, Alex Malyushytskyy wrote:

> Assume I have the following pseudo-code ( :
>
> vtkSmartPointer <Class> sptr = vtkSmartPointer <Class>::New();
> ....................
> // case a
> Class* p1 = Class::New();
>
> // case b
> vtkSmartPointer <Class> sptr2 = vtkSmartPointer <Class>::New();
> ............................
>
> What is the correct way to associate smart pointer with
> a) newly allocated pointer to the Class,
> b) pointer another smart pointer points to
>
> so currently associated  data is properly released and memory  
> deallocated?
> It looks like simple assigning does not work .
>
> for the case 'a' I thought that
>    sptr->Take(p1)
> should work, but it seems in may case previously associated data is
> not destroyed (destructor is not called), so I assume it is not
> dereferenced for some reason.
>
> for the case 'b'  my assumptions was that simple assigning should  
> work:
>
> sptr = sptr2;
>
>
> Thanks in advance,
>            Alex
> _______________________________________________
> 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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by David Doria-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 5, 2009 at 8:18 AM, Michael Jackson
<mike.jackson@...> wrote:
> case b.
> --
> Mike Jackson <www.bluequartz.net>

I tried to start a little tutorial on smart pointers:
http://www.vtk.org/Wiki/Smart_Pointers

Alex, I made you a slot to say something about this "Putting an
Existing Object into a Smart Pointer" (I'm not sure what else to call
it).

Can anyone verify what is there makes sense / add to it? Surely I am
just touching on the very basics and maybe missing some of the
idea/power of them...

Thanks,

David
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by John M. Drescher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I tried to start a little tutorial on smart pointers:
> http://www.vtk.org/Wiki/Smart_Pointers
>
Thanks. These tutorials are very helpful.

John
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Parent Message unknown Re: vtkSmartPointer usage question

by Alex Malyushytskyy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

David,

It looks like tutorial at
http://www.vtk.org/Wiki/Smart_Pointers
is the most detailed explanation I seen by now.

The only thing is  not clear  for me is how to force smartpointer to
release the data.

I may be missing the obvious, but at least I could not get it  from
documentation or class description.

"  // Description:
  // Assign object to reference.  This removes any reference to an old
  // object.
  vtkSmartPointer& operator=(T* r)"

What happens to the old reference?
I can assume that reference count will be decreased. Will it or not?
Would pointer be deleted if its reference count get to 0 when de -referenced ?
Logically I would answer positive on both questions, but that are my
assumptions.
I can try to see if they are correct by testing, but I would prefer to
see it somewhere in the documentation, so you can be sure how code
supposed to work.


For example stl auto_ptr  class provides "release" member, which
replaces the stored pointer  with a null pointer and returns the
previously stored pointer.

Basically this means that you get a normal pointer which you can
handle yourself.
And auto_ptr which can be used for other purposes (usually associated
with different raw pointer).

Such functionality is useful when you have smart pointers as class
members and need to update data due to some changes.

As for me as soon "How to release" is clarified, there is no need in
"Putting an
Existing Object into a Smart Pointer", cause this assumes two
operations: release and assign,
and "assign" is already covered in your guide.


Thanks for a great work,

  Alex


>>
>>Alex, I made you a slot to say something about this "Putting an
>>Existing Object into a Smart Pointer" (I'm not sure what else to call
>>it).
>>
>>Can anyone verify what is there makes sense / add to it? Surely I am
>>just touching on the very basics and maybe missing some of the
>>idea/power of them...
>>
>>Thanks,
>>
>>David
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by Elvis Dowson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

On Nov 6, 2009, at 2:50 AM, Alex Malyushytskyy wrote:

> What happens to the old reference?
> I can assume that reference count will be decreased. Will it or not?
> Would pointer be deleted if its reference count get to 0 when de -
> referenced ?

When the reference count is not zero, and you call release, the object  
is not deleted.

When the reference count hits zero, after you call release, it is safe  
to delete the object.

Best regards,

Elvis
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by Jeff Baumes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The only thing is  not clear  for me is how to force smartpointer to
release the data.


Is this what you're looking for?

vtkSmartPointer<vtkType> sp = vtkSmartPointer<vtkType>::New();
...
sp = NULL; // Release would happen here.

This would decrement the reference count of the object by one. If no one else holds a reference count of the object (i.e. reference count goes to zero), it will be released from memory.

Normally you just wait for the smart pointer to go out of scope. If you explicitly need to make the reference go away at a certain point, you might as well not use smart pointers and just do this equivalent code:

vtkType* p = vtkType::New();
...
p->Delete();

Jeff

_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by Alex Malyushytskyy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jeff,

> sp = NULL; // Release would happen here.
That was what I was looking for.
When I tried to look at the code I forgot that vtkSmartPointer is
dialing with vtkObjectBase pointers.

> Normally you just wait for the smart pointer to go out of scope.

Main reason for me to use smartpointers - it helps dealing with
exceptions especially thrown from constructors.
So I usually use smart pointers as a class members instead of raw pointers .
And even though smartpointer will do his job when it goes out of scope,
you may get situation when you need to replace the data when it does not.

Example:
Assume poly data is read from the file and stored with smartpointer.
User action changes the data (for example some additional tessellation).
If result is satisfactory, old data should be replaced.

Thanks everybody for help,
             Alex




On Fri, Nov 6, 2009 at 5:53 AM, Jeff Baumes <jeff.baumes@...> wrote:

>> The only thing is  not clear  for me is how to force smartpointer to
>> release the data.
>>
>
> Is this what you're looking for?
> vtkSmartPointer<vtkType> sp = vtkSmartPointer<vtkType>::New();
> ...
> sp = NULL; // Release would happen here.
> This would decrement the reference count of the object by one. If no one
> else holds a reference count of the object (i.e. reference count goes to
> zero), it will be released from memory.
> Normally you just wait for the smart pointer to go out of scope. If you
> explicitly need to make the reference go away at a certain point, you might
> as well not use smart pointers and just do this equivalent code:
> vtkType* p = vtkType::New();
> ...
> p->Delete();
> Jeff
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by F-18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've been following along with this conversation and following the example on the wiki, I tried to do this:

    vtkSmartPointer<vtkMultiBlockPLOT3DReader> m_Reader2;
    vtkSmartPointer<vtkMultiBlockDataSet> dataset = m_Reader2->GetOutput();

which gave me an error
 error C2664: 'vtkSmartPointerBase::vtkSmartPointerBase(vtkObjectBase *)' : cannot convert parameter 1 from 'vtkMultiBlockDataSet *' to 'vtkObjectBase *'

I can't seem to figure out why this is the case?


On Fri, Nov 6, 2009 at 4:03 PM, Alex Malyushytskyy <alexmalvtk@...> wrote:
Jeff,

> sp = NULL; // Release would happen here.
That was what I was looking for.
When I tried to look at the code I forgot that vtkSmartPointer is
dialing with vtkObjectBase pointers.

> Normally you just wait for the smart pointer to go out of scope.

Main reason for me to use smartpointers - it helps dealing with
exceptions especially thrown from constructors.
So I usually use smart pointers as a class members instead of raw pointers .
And even though smartpointer will do his job when it goes out of scope,
you may get situation when you need to replace the data when it does not.

Example:
Assume poly data is read from the file and stored with smartpointer.
User action changes the data (for example some additional tessellation).
If result is satisfactory, old data should be replaced.

Thanks everybody for help,
            Alex




On Fri, Nov 6, 2009 at 5:53 AM, Jeff Baumes <jeff.baumes@...> wrote:
>> The only thing is  not clear  for me is how to force smartpointer to
>> release the data.
>>
>
> Is this what you're looking for?
> vtkSmartPointer<vtkType> sp = vtkSmartPointer<vtkType>::New();
> ...
> sp = NULL; // Release would happen here.
> This would decrement the reference count of the object by one. If no one
> else holds a reference count of the object (i.e. reference count goes to
> zero), it will be released from memory.
> Normally you just wait for the smart pointer to go out of scope. If you
> explicitly need to make the reference go away at a certain point, you might
> as well not use smart pointers and just do this equivalent code:
> vtkType* p = vtkType::New();
> ...
> p->Delete();
> Jeff
_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers


_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by Jeff Baumes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Fri, Nov 6, 2009 at 6:17 PM, F <frinxor@...> wrote:
I've been following along with this conversation and following the example on the wiki, I tried to do this:

    vtkSmartPointer<vtkMultiBlockPLOT3DReader> m_Reader2;
    vtkSmartPointer<vtkMultiBlockDataSet> dataset = m_Reader2->GetOutput();

which gave me an error
 error C2664: 'vtkSmartPointerBase::vtkSmartPointerBase(vtkObjectBase *)' : cannot convert parameter 1 from 'vtkMultiBlockDataSet *' to 'vtkObjectBase *'

I can't seem to figure out why this is the case?



These seemingly cryptic errors with smart pointers normally mean that you forgot an include:

#include "vtkMultiBlockDataSet.h"

Jeff 


_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers

Re: vtkSmartPointer usage question

by F-18 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Jeff,

that fixed it! I had double checked on that at first, but for some reason thought vtkMultiBlockPLOT3DReader was the equivalent of vtkMultiBlockDataSet...

On Fri, Nov 6, 2009 at 6:20 PM, Jeff Baumes <jeff.baumes@...> wrote:


On Fri, Nov 6, 2009 at 6:17 PM, F <frinxor@...> wrote:
I've been following along with this conversation and following the example on the wiki, I tried to do this:

    vtkSmartPointer<vtkMultiBlockPLOT3DReader> m_Reader2;
    vtkSmartPointer<vtkMultiBlockDataSet> dataset = m_Reader2->GetOutput();

which gave me an error
 error C2664: 'vtkSmartPointerBase::vtkSmartPointerBase(vtkObjectBase *)' : cannot convert parameter 1 from 'vtkMultiBlockDataSet *' to 'vtkObjectBase *'

I can't seem to figure out why this is the case?



These seemingly cryptic errors with smart pointers normally mean that you forgot an include:

#include "vtkMultiBlockDataSet.h"

Jeff 



_______________________________________________
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

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers