Results 1 to 5 of 5

Thread: [RESOLVED] Quick GDI Memory Disposal Question

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Quick GDI Memory Disposal Question

    There's no difference. That first code snippet could be written like this:
    vb.net Code:
    1. Dim F As Font
    2.  
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Quick GDI Memory Disposal Question

    Quote Originally Posted by neef View Post
    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 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    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
  •  



Click Here to Expand Forum to Full Width