Announcement

Collapse
No announcement yet.

API Question: Resource Leak Using Bitmap

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • API Question: Resource Leak Using Bitmap

    The following series of commands are leaking a little GDI memory every time they are called. Something is not being deleted. Any ideas as to how to correctly delete the DC and bitmaps?


    ' Create hidden surface, m_ctrlhDC never changes through the
    ' life of the program
    m_hDC = CreateCompatibleDC(m_ctrlhDC)

    'To draw to the hidden surface need to create a bitmap
    m_hBMP = CreateCompatibleBitmap(m_ctrlhDC, m_ctrlWidth, m_ctrlHeight)

    'Attach Bitmap to hidden surface
    m_OldhBMP = SelectObject(m_hDC, m_hBMP)

    Local hBmp As Long
    'First deselect bitmap
    hBmp = SelectObject(m_hDC, m_OldhBMP)

    Call DeleteDC(m_hDC)
    Call DeleteObject(m_hBMP)
    Call DeleteObject(hBmp)

  • #2
    If you consider these lines of code:
    Code:
    m_OldhBMP = SelectObject(m_hDC, m_hBMP)
    ...
    hBmp = SelectObject(m_hDC, m_OldhBMP)
    ...
    Call DeleteObject(m_hBMP)
    Call DeleteObject(hBmp)
    Then hBmp would be equal to m_hBmp, so the last two calls are performing an identical task, and hence the 1st should success, but the second has to fail (as the same handle was just released by the 1st DeleteObject() call).

    Therefore the 1st question is this: what are the return results from these API's (DeleteObject(), DeleteDC(), etc).

    If they are returning *valid* return values to indicate that they were successful (and at least one of these you listed should not work as I mentioned above), then the problem is somewhere else in your code that you have no given us. FOr example, maybe you arew drawing on the memory bitmap, and you are attempting to release a pen or brush that is still selected into the DC? If you verify the return result from every API, you'll find the cause (provided you match the destruction of every GDI object to it's creation).

    I hope this helps.

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      You are right that last line fails, it returns a zero. The SelectObject returns a handle and the DeleteDC and first DeleteDC returns a 1 so they are working.

      It must be in the drawing code. . .

      [This message has been edited by Tim Wisseman (edited June 12, 2000).]

      Comment


      • #4
        You were right again Lance,

        I was making the mistake of trying to delete a pen that was attached to a device context.

        All that I had to do was change the line:

        Call DeleteObject(m_hPen)

        To:

        Call DeleteObject(SelectObject(m_hDC, GetStockObject BLACK_PEN)))

        . . . and the leak is gone!

        The leak was happening because I was deleting the DC and it had a pen still attached to it, because DeleteObject(m_hPen) was failing due to the pen still being attached to the DC.

        So, aways replace the Pen with a stock object and then delete the Pen. . . I did not know that you had to do that.

        Thanks Lance!

        Tim



        [This message has been edited by Tim Wisseman (edited June 12, 2000).]

        Comment


        • #5
          The best "rule" is to store the original object handle (brush, pen, etc) at the 1st selection, and just before deleteing or releasing the DC, reselect the original object(s).

          Locating which GDI object is leaking is not always easy, but ignoring API return codes is a sure-fire way of overlooking the real cause of a leak.

          Once bitten...



          ------------------
          Lance
          PowerBASIC Support
          mailto:[email protected][email protected]</A>
          Lance
          mailto:[email protected]

          Comment

          Working...
          X