Garbage Collection question

  • Thread starter Thread starter Mike Grace
  • Start date Start date
M

Mike Grace

Hi,

If I have an object which internally creates a few font objects, will the
font objects be disposed automatically when the main object is disposed i.e.
goes out of scope, or will it be stuck and be a memory eater?

Regards


Mike
 
Hi Mike,

since Font implements IDisposable Dispose() has to be called explicitly to
clean up unmanaged resources associated with the Font instance.
In C# you can also use the using keyword to call Dispose() implicitly:

using (Font font = new Font(...)) {
...
}

- Dennis
 
Dennis Homann said:
Hi Mike,

since Font implements IDisposable Dispose() has to be called explicitly to
clean up unmanaged resources associated with the Font instance.
In C# you can also use the using keyword to call Dispose() implicitly:

using (Font font = new Font(...)) {
...
}

RULE: Any type aggregating a disposable type should be disposable.

If Font should be disposed, and you have aggregated it, then your object
should become disposable.
Responsibility for disposing the fonts then passes out to the clients of the
aggregating type.


class myType
{
Font font1 = new Font(...);
Font font2 = new Font(...);

void Dispose()
{
font1.Dispose();
font2.Disppose();
}

}

David
 
Ah O.K.

Thanks guys.

Mike

David Browne said:
RULE: Any type aggregating a disposable type should be disposable.

If Font should be disposed, and you have aggregated it, then your object
should become disposable.
Responsibility for disposing the fonts then passes out to the clients of the
aggregating type.


class myType
{
Font font1 = new Font(...);
Font font2 = new Font(...);

void Dispose()
{
font1.Dispose();
font2.Disppose();
}

}

David
 
Back
Top