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

Miller, James V (Research) millerjv at crd.ge.com
Tue May 21 13:15:52 EDT 2002


Doesn't Delete() just all UnRegister() if it is not the last reference?
Perhaps all that is needed is call Register() in the constructor of 
vtkAutoDelete.

-----Original Message-----
From: Andy Cedilnik [mailto:andy.cedilnik at kitware.com]
Sent: Tuesday, May 21, 2002 7:49 AM
To: Brad King
Cc: VTK Developers
Subject: Re: [vtk-developers] Calling foo->Delete() automatically.


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
> 


_______________________________________________
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