|
View:
New views
11 Messages
—
Rating Filter:
Alert me
|
|
|
vtkSmartPointer usage questionAssume 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 questioncase 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 questionOn 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> 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 |
|
|
|
|
|
Re: vtkSmartPointer usage questionHi,
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 questionThe only thing is not clear for me is how to force smartpointer to 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 questionJeff,
> 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 questionI'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, _______________________________________________ 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 questionOn 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: 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 questionThanks 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:
_______________________________________________ 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 |
| Free embeddable forum powered by Nabble | Forum Help |