Will Dispose() be called by GC?
-
Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?
class My
{
public void Dispose()
{
// ...
}
}I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.
Чесноков
-
Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?
class My
{
public void Dispose()
{
// ...
}
}I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.
Чесноков
The runtime won't call it, but will it be confusing to future people looking at the code?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?
class My
{
public void Dispose()
{
// ...
}
}I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.
Чесноков
The GC calls
Dispose
methods only onIDisposable
objects. So, in your case GC will not call the Dispose method. Although it is not illegal to use the name 'Dispose' for your cleanup methods, other developers who work with your code may get confused. EDIT: I meant to say the that the GC calls the finalizer which in turn is supposed to call Dispose method. The GC does NOT call Dispose method directly even if the class implements IDisposable interface. However, in ausing
code block, the C# compiler checks if the class implements IDisposable interface when compiling the code into a try...finally... block and calls itsDispose
method in thefinally
block.modified on Wednesday, May 18, 2011 9:44 AM
-
Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?
class My
{
public void Dispose()
{
// ...
}
}I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.
Чесноков
As it stands, Dispose is just a method name. The trick with IDisposable objects is that the interface is well known, so it can be accessed using (effectively):
IDisposable = thisClass as IDisposable;
if (thisClass != null)
thisClass.Dispose();Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
As it stands, Dispose is just a method name. The trick with IDisposable objects is that the interface is well known, so it can be accessed using (effectively):
IDisposable = thisClass as IDisposable;
if (thisClass != null)
thisClass.Dispose();Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?
class My
{
public void Dispose()
{
// ...
}
}I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.
Чесноков
Maybe, if it's called from the finalizer. But I would implement the IDisposable interface, it's free.
-
Shouldn't the code be something like this?
IDisposable i = thisClass as IDisposable;
if (i != null) {
i.Dispose();
}It should. That's what I get for typing replies on a phone.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
It should. That's what I get for typing replies on a phone.
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility