[vtk-developers] Memory leak in vtkSmartPointer

Utkarsh Ayachit utkarsh.ayachit at kitware.com
Thu Jun 24 09:31:27 EDT 2010


What version of VTK are you using? Are you sure you are looking at the
correct reference count. The following code always prints out 1 for
me, which is what is expected.

   vtkSmartPointer<vtkObject> foo;
   foo.TakeReference( vtkObject::New() );
  cout << foo->GetReferenceCount() << endl;

  vtkSmartPointer<vtkObject> obj;
  obj = vtkSmartPointer<vtkObject>::New();
  cout << obj->GetReferenceCount() << endl;

Utkarsh

On Thu, Jun 24, 2010 at 4:18 AM, Nasztanovics Ferenc <naszta at naszta.hu> wrote:
> Hi,
>
> I debugged the following code (Is it a correct use case?):
> vtkSmartPointer<vtkSomething> foo;
> foo.TakeReference( vtkSomething::New() );
>
> When TakeReference run:
> 1) vtkSomething::New() - reference number grow (1)
> 2) TakeReference call: vtkSmartPointer<T>(t, NoReference()); - constructor, no reference count growing (1)
> 3) TakeReference call: vtkSmartPointerBase::operator=(CheckType(r.GetPointer())); - reference number grow (2)
>
> when I leave the block UnRegister(NULL) called just once -> this instance of vtkSomething object has left in memory.
>
> I debugged the following code, too (Is it a correct use case?):
> vtkSmartPointer<vtkSomething> foo;
> foo = vtkSmartPointer<vtkSomething>::New() );
>
> 1) vtkSmartPointer<T>(T::New(), NoReference()); - reference count grow (1)
> 2) vtkSmartPointerBase::operator=(CheckType(r.GetPointer())); - reference number grow (2)
>
> when I leave the block UnRegister(NULL) called just once -> this instance of vtkSomething object has left in memory.
>
> Platform: Linux,
> CPU: G5 PowerPC
> GCC: 4.3.2
>
> Best regards,
>
> Ferenc Nasztanovics
>
> ________________________________________
> Feladó: vtk-developers-bounces at vtk.org [vtk-developers-bounces at vtk.org], meghatalmazó: Nasztanovics Ferenc [naszta at naszta.hu]
> Küldve: 2010. június 23. 12:12
> Címzett: vtk-developers at vtk.org
> Tárgy: [vtk-developers] Memory leak in vtkSmartPointer
>
> Hi,
>
> In vtkSmartPointer we have the following code:
>
> static T* CheckType(T* t) { return t; }
> template <class U>
> vtkSmartPointer& operator=(const vtkSmartPointer<U>& r)
>  {
>  this->vtkSmartPointerBase::operator=(CheckType(r.GetPointer()));
>  return *this;
>  }
>
> void TakeReference(T* t)
>  {
>  *this = vtkSmartPointer<T>(t, NoReference());
>  }
>
> ------------------
>
> In my code I use
>
> vtkSmartPointer<vtkSomething> foo;
> foo.TakeReference( vtkSomething::New() );
>
> the reference count is 2. I tried the following code, too:
>
> vtkSmartPointer<vtkSomething> foo;
> foo = vtkSmartPointer<vtkSomething>::New() );
>
> but the reference number is still 2.
>
> Best regards,
>
> Ferenc Nasztanovics
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtk-developers
>
>



More information about the vtk-developers mailing list