[vtk-developers] Calling foo->Delete() automatically.

Andy Cedilnik andy.cedilnik at kitware.com
Tue May 21 07:48:36 EDT 2002


I like the idea, but I see a small problem. There are several people out
there who will look at this code briefly and will not understand where
the pointers get deleted. They might assume that there is some garbage
collection or something. 

Another thing, this only works for New/Delete pair. What about register
and unregister? 

			Andy

On Wed, 2002-05-15 at 16:53, Brad King wrote:
> Hello, all:
> 
> Recently I've been switching often between developing VTK classes and
> developing projects that use smart pointers.  As a result, I've
> created many leaks by forgetting to add "foo->Delete()" at all exit
> points of a method.
> 
> One intention of C++'s automatic variable destruction feature is for
> resource cleanup.  I propose the following short class for inclusion
> in vtkSetGet or another high-level header:
> 
> class vtkAutoDelete
> {
> public:
>   vtkAutoDelete(vtkObject* obj): Object(obj) {}
>   ~vtkAutoDelete() { if(this->Object) { this->Object->Delete(); } }
> private:
>   vtkObject* Object;
> };
> 
> An instance of this class exists only to call Delete() on an object at
> all exit points in a method that owns a reference to that object.
> Here is an example use:
> 
>   vtkFoo* foo = vtkFoo::New();
>   vtkAutoDelete delete_foo(foo);
>   ... setup foo ...
>   if(error)
>     {
>     vtkErrorMacro(...);
>     return 0; // foo automatically deleted.
>     }
>   ... use foo ...
>   if(other_error)
>     {
>     vtkErrorMacro(...);
>     return 0; // foo automatically deleted.
>     }
>   return 1; // foo automatically deleted.
> 
> This idea is similar to smart pointers, but it is completely optional and
> requires no changes to existing code.  It is really a VTK version of the
> C++ standard's "auto_ptr" class.
> 
> I'm interested in the list's comments.
> 
> -Brad
> 
> _______________________________________________
> vtk-developers mailing list
> vtk-developers at public.kitware.com
> http://public.kitware.com/mailman/listinfo/vtk-developers
> 





More information about the vtk-developers mailing list