AUTODISPOSE (Auto Disposal of Objects)

Top  Previous  Next

 

AUTODISPOSE

 

AUTODISPOSE is used when declaring an instance of any target object.

 

For all objects declared in the .NET Framework environment, they are automatically disposed using a .NET algorithm named Garbage Collection (GC). So why use AUTODISPOSE?

 

AUTODISPOSE is useful in cases where we want deterministic resource cleanup. Examples are closing file streams, releasing bitmaps, etc. and any unmanaged resources that should be disposed. For example, a Bitmap object that encapsulates a 4MB image file reports only the size of the managed object to the .NET Garbage Collector, but the Garbage Collector doesn't know about the 4MB image, so it won't be handled. When you call DISPOSE() on the Bitmap object it will release the image itself. AUTODISPOSE is just a shortcut, as the compiler will automatically generate the call to DISPOSE() for you.

AUTODISPOSE is also useful when working with classes that implement the IDisposable interface, like TextReader. By using the AUTODISPOSE attribute in a variable declaration, if the variable type implements IDisposable, DISPOSE will be called when the variable becomes out of scope.

Example:

 

MyObject     AnyObject, AUTODISPOSE

 

An equivalent of this C# code in Clarion#

 

USING (MyDialog md = new MyDialog())

{

some code...

}

 

is this:

 

Myclass.proc1 Procedure()

fOpenDialog OpenFileDialog(), AUTODISPOSE

CODE

fOpenDialog.ShowDialog()

 

The AUTODISPOSE attribute on a local procedure variable means that the Dispose() method will automatically be called for it before exit from a procedure.