[vtkusers] using vtkSmartPtrs
Sean McInerney
seanm at nmr.mgh.harvard.edu
Tue Aug 24 22:21:54 EDT 2004
Eric,
You may be overlooking *all* incrementing of your object's reference
count. vtkSmartPtr increments its object's reference count on
construction and decrements it on destruction, but you may be forgetting
that a vtkObject sets its own reference count to 1 in its constructor.
e.g.
void f()
{
vtkSmartPtr<vtkObject> ptr(vtkObject::New());
// Even though the smart pointer is destroyed when this function
// goes out of scope and consequently decrements the reference count
// of the object created above, the object itself is not destroyed.
// Its reference count is still 1 and, therefore, a memory leak!
}
To avoid such a leak, the above function should be composed like this:
void f()
{
vtkObject* object = vtkObject::New();
vtkSmartPtr<vtkObject> ptr(object);
object->Delete(); // The object's reference count is now 1
// The smart pointer as well as the object that it references will
// be destroyed when this function goes out of scope.
}
Hopefully, your woes are a simple matter of book keeping. If you are
already addressing the issue that I have outlined, something more
sinister may be at work.
-Sean
Harlow Eric D Contr 412 TW/ENR wrote:
> 1. How common is it to wrap vtk objects in vtkSmartPtrs? This is causing a
> memory leak in our code.
>
> vtkSmartPtr<OpenGLRenderWindow> RenderWindow_;
>
>
> 2. We are placing all of the VTK functions in our VTK Application in a C++
> dll library so that the end user doesn't have to be vtk expert. Are there
> some VTK developers out there that have had success in wrapping the VTK
> member functions/objects in a C++ MFC dll? It was easy enough when we were
> building our test Application by calling the vtk functions either from the
> MFC ViewClass or classes in the Application project space.
>
> Thanks
>
> -Eric
More information about the vtkusers
mailing list