[vtkusers] vtkSmartPointer BUG

David Cole david.cole at kitware.com
Tue Oct 7 11:02:40 EDT 2008


The assignment operator works by increasing the ref count of the object
pointed to. But the object returned from the raw New has a ref count on it
already, so combining it with the assignment operator results in a ref count
of 2. If you use the raw New you have to save its value in a local
non-smart-pointer variable and then Delete it when you are done with it.
(That's the whole reason for having a smart pointer specific New method is
to avoid that hassle.)

Does that make sense?

David


On Tue, Oct 7, 2008 at 10:58 AM, pjtr hahn <pjtr.hahn at googlemail.com> wrote:

> Hmm, Ok I didn't knew the usage you show in your example but
> netherless the docs have an assignment operator that should work as I
> expected, or am I wrong?
>
> vtkSmartPointer & operator= (T *r)
>
>
> Thanks so far for your hint and example.
>
>
>
> 2008/10/7 David Cole <david.cole at kitware.com>:
> > I am not sure why that worked before...... It should have given you a
> memory
> > leak before. Maybe you didn't really have leak detection turned on
> before?
> > The correct New construct to use with vtkSmartPointer is:
> >   vtkSmartPointer<vtkSomeObject> ptr
> > = vtkSmartPointer<vtkSomeObject>::New();
> > HTH,
> > David
> >
> > On Tue, Oct 7, 2008 at 9:39 AM, pjtr hahn <pjtr.hahn at googlemail.com>
> wrote:
> >>
> >> Hello,
> >>
> >> since my last CVS Update (today)  vtkSmartPointer isn't working like
> >> expected anymore.
> >>
> >> While code like
> >>
> >> function_scope{
> >>
> >> vtkSomeObject * ptr = vtkSomeObjekt::New();
> >>
> >> ...... more code ......
> >>
> >> ptr->Delete();
> >>
> >> }
> >>
> >> works I get memory leak error messages when changing the same code to
> >>
> >> function_scope{
> >>
> >> vtkSmartPointer< vtkSomeObject > ptr = vtkSomeObjekt::New();
> >>
> >> ...... more code ......
> >>
> >> } // end scope
> >>
> >> This worked before.
> >>
> >> I'm on Vista / Visual Studio Express 2008
> >>
> >> regards,
> >>
> >> Peter
> >> _______________________________________________
> >> 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
> >
> >
> _______________________________________________
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20081007/909c71f0/attachment.htm>


More information about the vtkusers mailing list