|
-
Jul 14th, 2019, 08:14 AM
#1
Thread Starter
Hyperactive Member
[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
Last edited by neef; Jul 14th, 2019 at 02:08 PM.
Intermediate Level Programmer Extraordinaire 
-
Jul 14th, 2019, 09:33 AM
#2
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.
-
Jul 14th, 2019, 10:18 AM
#3
Thread Starter
Hyperactive Member
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?
Intermediate Level Programmer Extraordinaire 
-
Jul 14th, 2019, 01:54 PM
#4
Re: Quick GDI Memory Disposal Question
 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.
 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.
-
Jul 14th, 2019, 02:07 PM
#5
Thread Starter
Hyperactive Member
Re: Quick GDI Memory Disposal Question
Very helpful... thank you.
Intermediate Level Programmer Extraordinaire 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|