member variable gets disposed?

I have a class
public class SingleInstance
{
Mutex m;
...
and I thought it might be a good idea to release sometime
~SingleInstance()
{
m.ReleaseMutex();
}

however, I get the error 'cannot access disposed object'
because the second instance I guess is holding the same object as the first instance?

but how can something be disposed when there is still a reference to it?
[452 byte] By [FoodBard] at [2007-11-19 7:32:00]
# 1 Re: member variable gets disposed?
OF COURSE you instantiated Mutex m, yes?

Some sort of

public class SingleInstance
{
Mutex m;

SingleInstance() {
m = new Mutex();
}

Ciiiiiiiiiiiiiiiiiiiiiiiii...
cicciodinoto at 2007-11-9 1:46:43 >
# 2 Re: member variable gets disposed?
the constructor creates a new item and starts the form if it was able to get the lock
FoodBard at 2007-11-9 1:47:35 >
# 3 Re: member variable gets disposed?
The problem with destructors in C# (in .NET framework actually) is that you don't know whey they are called. Specifically, they're called not long before the object is garbage collected. When will that be? When the system gets around to it. You can ask it to do a garbage collection, but that's usually not what you want to doand doing so isn't guaranteed to call all the finalizers right away.

So, if you need to release some critical resource right away you should wirte a Dispose() method to implement IDisposable and take responsibility for calling it when you're done with your object.
cilu at 2007-11-9 1:48:45 >
# 4 Re: member variable gets disposed?
actually, I wanted it to be released when the form was closed and garbage collected as the application exits.

because I cannot force the users to call the dispose when they finish
FoodBard at 2007-11-9 1:49:39 >
# 5 Re: member variable gets disposed?
Cilu is right and you are definitely wrong achieving your goal this way. As far as I know garbage collector, you cannot suceeded.

The best solution is implement Dispose pattern (IDisposable and Dispose(); look upon it in MSDN).

You are talking aobut forms so I guess you are using WinForms. If that's true, you are lucky man. Because WinForms automaticaly calls Dispose() on forms beeing closed. So you can call Dispose() od your class in Dispose() of form which use it.

And there is another way: use using statement:
using (SingleInstance si = new SingleInstance())
{
...
} // !!

If SingleInstance implements IDisposable(), than after leaving } of using (tagged with // !!), Dispose() of it is called.
boudino at 2007-11-9 1:50:38 >
# 6 Re: member variable gets disposed?
okay, when WinForms automaticaly calls Dispose(), will it automatically call it on all the member variables?

also, since dispose and the garbage collectors seem to happen before the destructor, what kind of stuff should go in the destructor :confused:
FoodBard at 2007-11-9 1:51:42 >
# 7 Re: member variable gets disposed?
okay, when WinForms automaticaly calls Dispose(), will it automatically call it on all the member variables?

No, you have to do it manualy in Dispose() (Maybe it will call it on those in this.components, but I am not sure. This collection is violated by visual designer.)

also, since dispose and the garbage collectors seem to happen before the destructor, what kind of stuff should go in the destructor :confused:

None. You should not worry about destructors. Remeber - you are using C#, not C++. :)
boudino at 2007-11-9 1:52:44 >
# 8 Re: member variable gets disposed?
just when you have some unmanaged resources you need to implement IDisposable..or sometimes you have some managed resources nad you want to dispose them as soon as possible(like a pen because we may need use a pen manytimes just in one second so it should be manually disposed by us instead of garbage collector man),
in other situations you won't need to dispose your managed resources. :)
mehdi62b at 2007-11-9 1:53:50 >