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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

garbage collection 3
Design Question 3
object dispose 2
Events and Garbage collection 1
keeping RCW afloat 8
GC and Dispose 6
Garbage collector question 11
BitmapSourceSafeMILHandle, SafeMILHandle never disposed 1

Back
Top