[RESOLVED] Quick GDI Memory Disposal Question
Self-taught programmer here... I have some gaps in some of my learning I'm trying to fill. Lately the gaps are in memory management, so forgive the basicness of this question:
If I declare some sort of GDI object, say a font like this:
dim F as New Font("Arial", 27, FontStyle.Bold)
I can dispose it easily when I am done with it using F.Dispose
But what if I create a font like this?
SomeButton.Font = New Font("Arial", 27, FontStyle.Bold)
How do you dispose of something that is created on the fly like this?
Thanks!
neef
Re: Quick GDI Memory Disposal Question
There's no difference. That first code snippet could be written like this:
vb.net Code:
Dim F As Font
F = New Font("Arial", 27, FontStyle.Bold)
As you can see, there's no difference between that second line and your second code snippet, so the way you dispose the object is no different either. You simply call Dispose on whatever you assigning the Font object to in the first place.
Re: Quick GDI Memory Disposal Question
So the correct syntax would be SomeButton.Font.Dispose(), correct? And I should do that for every font I create using the "new" keyword?
What about disposing font objects associated with buttons/textboxes/ etc... created in the designer? Is that taken care of automatically?
Re: Quick GDI Memory Disposal Question
Quote:
Originally Posted by
neef
So the correct syntax would be SomeButton.Font.Dispose(), correct? And I should do that for every font I create using the "new" keyword?
Correct. You should take responsibility for disposing everything disposable that you create.
Quote:
Originally Posted by
neef
What about disposing font objects associated with buttons/textboxes/ etc... created in the designer? Is that taken care of automatically?
Correct. You generally don't need to take responsibility for disposing things that you don't create and anything designer-generated should be taken care of automatically, assuming that the form itself is disposed correctly.
Re: Quick GDI Memory Disposal Question
Very helpful... thank you.