Generic Dispose Function?

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

In C++ I used to declare the following macro for deleteing objects:

#define SAFEDELETE(x) if(x != NULL) { delete x; x = NULL; }

Is there currently a way to do something similar to this in C#?

I'm constantly writing...

if(x != null)
{
x.Dispose();
x = null;
}

and would love to just write something like...

SAFEDISPOSE(x);

Thanks for your help!
 
Jeff said:
In C++ I used to declare the following macro for deleteing objects:

#define SAFEDELETE(x) if(x != NULL) { delete x; x = NULL; }

Is there currently a way to do something similar to this in C#?

I'm constantly writing...

if(x != null)
{
x.Dispose();
x = null;
}

and would love to just write something like...

SAFEDISPOSE(x);


automatic calling of "Dispose" is provided via "using" statement:

<code>
using(StreamWriter swr = new StreamWriter("\\txts.txt", false))
{
swr.Write("Hello World!");
}
</code>


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 
Jeff said:
In C++ I used to declare the following macro for deleteing objects:

#define SAFEDELETE(x) if(x != NULL) { delete x; x = NULL; }

Is there currently a way to do something similar to this in C#?

I'm constantly writing...

if(x != null)
{
x.Dispose();
x = null;
}

and would love to just write something like...

SAFEDISPOSE(x);

I can't say I regularly need that kind of thing at all. Does your use
case prohibit you from using the "using" construct, e.g.

using (Stream x = new FileStream(...))
{
// Use x here
} // Dispose is automatically called here

(I also can't immediately see the use of testing whether or not it's
null, then setting it to null afterwards.)
 
These objects are fields within my classes so I can not use the using
statement. I want to allow the users of my class to be able to dispose of
the class (and these fields) by calling dispose on my class. That is why I
am calling their dispose methods.

The reason I check for null before I dispose of them is because some objects
may have not been created yet and it would throw an exception if I tried to
call Dispose on an object that was null.

Is there a better way to do this?
 
Jeff said:
These objects are fields within my classes so I can not use the using
statement. I want to allow the users of my class to be able to dispose of
the class (and these fields) by calling dispose on my class. That is why I
am calling their dispose methods.

The reason I check for null before I dispose of them is because some objects
may have not been created yet and it would throw an exception if I tried to
call Dispose on an object that was null.

Is there a better way to do this?

Well, it wouldn't be hard to write a SafeDispose method:

static void SafeDispose (IDisposable disp)
{
if (disp != null)
{
disp.Dispose();
}
}

Personally I very rarely have objects which implement IDisposable as
instance fields, but maybe that's just me...
 
Jeff said:
These objects are fields within my classes so I can not use the using
statement. I want to allow the users of my class to be able to dispose of
the class (and these fields) by calling dispose on my class. That is why I
am calling their dispose methods.

The reason I check for null before I dispose of them is because some objects
may have not been created yet and it would throw an exception if I tried to
call Dispose on an object that was null.

Is there a better way to do this?

You could maintain a container of IDisposables as an instance member of your
class. When you create an instance of an IDisposable that your class owns,
add a reference to the object into this container. In the Dispose method of
the class, iterate over the container and call Dispose for each item. This
way there is no need to check for nulls before disposing.

On another note, there really is no reason to check against NULL in C++ when
calling delete. You could safely rewrite your macro like so:

#define SAFEDELETE(x) { delete x; x = NULL; }

Regards,
Sami
www.capehill.net
 
Jeff said:
These objects are fields within my classes so I can not use the using
statement. I want to allow the users of my class to be able to dispose of
the class (and these fields) by calling dispose on my class. That is why I
am calling their dispose methods.

The reason I check for null before I dispose of them is because some objects
may have not been created yet and it would throw an exception if I tried to
call Dispose on an object that was null.

Is there a better way to do this?

You could maintain a container of IDisposables as an instance member of your
class. When you create an instance of an IDisposable that your class owns,
add a reference to the object into this container. In the Dispose method of
the class, iterate over the container and call Dispose for each item. This
way there is no need to check for nulls before disposing.

On another note, there really is no reason to check against NULL in C++ when
calling delete. You could safely rewrite your macro like so:

#define SAFEDELETE(x) { delete x; x = NULL; }

Regards,
Sami
www.capehill.net
 
Back
Top