Deleting a Pen
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Deleting a Pen

  1. #1
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374

    Deleting a Pen

    If I create a Pen with

    hpenl = CreatePen(PS_SOLID, 0, color1);

    and select that pen and then

    hpenl = CreatePen(PS_SOLID, 0, color2);

    and select that pen,

    Can I delete a single pen or will there be a second undeleted pen out in never-never land?

    Brian

  2. #2
    Join Date
    Feb 2003
    Location
    California
    Posts
    334
    All calls to CreatePen() need to be matched with a parallel call to DeleteObject().
    Henri Hein
    Principal Engineer, Propel
    Do not credit Propel with my views or opinions.

  3. #3
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    Windows GDI objects are a limited resource. If you don't free an
    object with a call to DeleteObject everytime you create a new
    object, then you will sometime run out of resources. This will
    cause problems with programs other than your own.

    Allocated GDI object are not freed when the program exits. These
    remain allocated until a reboot.

  4. #4
    Join Date
    Dec 2002
    Location
    Candia, NH
    Posts
    374
    That means it would be a very dumb thing to do as I have done above. There would be no way to delete the first pen.

    Brian

  5. #5
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Gyannea
    That means it would be a very dumb thing to do as I have done above. There would be no way to delete the first pen.

    Brian
    yes it would be, there are some gdi tracking tools, don't have my favorite on this machine, but I can probably post it later if you'd like, or the link to it, to breakdown the gdi allocations for your process. One of the things you should keep track of when doing unit testing blada blada...because accidents/mistakes do happen.

  6. #6
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    I wouldn't make the judgement call as to Dumb or not.

    But, it is required that you call DeleteObject for each Create.

    Failure to do so will result in the GDI resources being consumed.

    This is an error that is easy to make and many do - I have before.

    Suggestion:
    Keep each GDI resource in a separate variable. Call DeleteObject
    on each when no longer needed.

    Also:
    Whenever you select a new resource, be sure to keep the original
    Pen/Brush/etc. and reset it back when done.

  7. #7
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    gdiobj.exe (I think gstrecken posted this tool last year) there is another one I use...but I can't remember it for the life of me right now

    and as cvogt61457 stated, sh*t happens, it's just being vigilante in your unit testing to make sure, it doesn't get to the QA department or out in the field...

    /Zero defects is only impossible if you think it is....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center