Results 1 to 3 of 3

Thread: GDI Object leak??

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member half flung pie's Avatar
    Join Date
    Jun 2005
    Location
    South Carolina, USA
    Posts
    317

    Question GDI Object leak??

    I'm using a TransparentBlt sub I found elsewhere on the net. I have a timer that calls the sub a couple times per second, and apparently there is some sort of leak because after a while the program stops drawing and Task Manager reports that it is using 10,000 GDI Objects. Can someone please help me find the leak, as I have no knowledge of GDI objects.
    The TransparentBlt Sub
    VB Code:
    1. Sub TransparentBlt(dsthDC As Long, srchDC As Long, x As Integer, y As Integer, Width As Integer, Height As Integer, TransColor As Long)
    2.  
    3.  Dim maskDC As Long      'DC for the mask
    4.  Dim tempDC As Long      'DC for temporary data
    5.  Dim hMaskBmp As Long    'Bitmap for mask
    6.  Dim hTempBmp As Long    'Bitmap for temporary data
    7.  
    8.  'First, create some DC's. These are our gateways to associated
    9.  'bitmaps in RAM
    10.  maskDC = CreateCompatibleDC(dsthDC)
    11.  tempDC = CreateCompatibleDC(dsthDC)
    12.  
    13.  'Then, we need the bitmaps. Note that we create a monochrome
    14.  'bitmap here!
    15.  'This is a trick we use for creating a mask fast enough.
    16.  hMaskBmp = CreateBitmap(Width, Height, 1, 1, ByVal 0&)
    17.  hTempBmp = CreateCompatibleBitmap(dsthDC, Width, Height)
    18.  
    19.  'Then we can assign the bitmaps to the DCs
    20.  hMaskBmp = SelectObject(maskDC, hMaskBmp)
    21.  hTempBmp = SelectObject(tempDC, hTempBmp)
    22.  
    23.  'Now we can create a mask. First, we set the background color
    24.  'to the transparent color; then we copy the image into the
    25.  'monochrome bitmap.
    26.  'When we are done, we reset the background color of the
    27.  'original source.
    28.  TransColor = SetBkColor(srchDC, TransColor)
    29.  BitBlt maskDC, 0, 0, Width, Height, srchDC, 0, 0, vbSrcCopy
    30.  TransColor = SetBkColor(srchDC, TransColor)
    31.  'The first we do with the mask is to MergePaint it into the
    32.  'destination.
    33.  'This will punch a WHITE hole in the background exactly were
    34.  'we want the graphics to be painted in.
    35.  BitBlt tempDC, 0, 0, Width, Height, maskDC, 0, 0, vbSrcCopy
    36.  BitBlt dsthDC, x, y, Width, Height, tempDC, 0, 0, vbMergePaint
    37.  
    38.  'Now we delete the transparent part of our source image. To do
    39.  'this, we must invert the mask and MergePaint it into the
    40.  'source image. The transparent area will now appear as WHITE.
    41.  BitBlt maskDC, 0, 0, Width, Height, maskDC, 0, 0, vbNotSrcCopy
    42.  BitBlt tempDC, 0, 0, Width, Height, srchDC, 0, 0, vbSrcCopy
    43.  BitBlt tempDC, 0, 0, Width, Height, maskDC, 0, 0, vbMergePaint
    44.  
    45.  'Both target and source are clean. All we have to do is to AND
    46.  'them together!
    47.  BitBlt dsthDC, x, y, Width, Height, tempDC, 0, 0, vbSrcAnd
    48.  
    49.  'Now all we have to do is to clean up after us and free system
    50.  'resources..
    51.  DeleteObject (hMaskBmp)
    52.  DeleteObject (hTempBmp)
    53.  DeleteDC (maskDC)
    54.  DeleteDC (tempDC)
    55. End Sub

    My timer code
    VB Code:
    1. Private Sub Timer_Timer()
    2. On Error GoTo errhandle
    3. Dim i As Byte
    4.  Me.Cls
    5.  BitBlt Me.hdc, 0, 0, Me.Width, Me.Height, DesktopDC, 0, 0, vbSrcCopy
    6.  
    7.  If SpriteCount > 0 Then
    8.  For i = 1 To SpriteCount
    9.   If Sprite(i).Shown Then
    10.    With Sprite(i)
    11.    picCurrentSprite.Cls
    12.    If .Reversed Then StretchBlt picCurrentSprite.hdc, .Width - 1, 0, -.Width, .Height, .hDCofRefPic, .xSrc, .ySrc, .Width, .Height, vbSrcCopy
    13.    If Not .Reversed Then BitBlt picCurrentSprite.hdc, 0, 0, .Width, .Height, .hDCofRefPic, .xSrc, .ySrc, vbSrcCopy
    14.    TransparentBlt Me.hdc, picCurrentSprite.hdc, .Left, .Top, .Width, .Height, GetPixel(.hDCofRefPic, 1, 1)
    15.    If .Reversed Then .Left = .Left + .WalkSpeed
    16.    If Not .Reversed Then .Left = .Left - .WalkSpeed
    17.    .ySrc = .ySrc + .Height
    18.    If .ySrc >= .TotalHeight Then .ySrc = 0
    19.    If .Left + .Width < 0 Then .Shown = False
    20.    If .Left > (Screen.Width / Screen.TwipsPerPixelX) Then .Shown = False
    21.    End With
    22.   End If
    23.  Next i
    24.  End If
    25.  
    26.  Exit Sub
    27. errhandle:
    28.  Debug.Print Err.Description
    29.  End
    30. End Sub

    Edit: I'm pretty sure the leak spawns from the TransparentBlt sub, because I commented out the call to that sub in my timer code, and there is no longer a leak.
    Last edited by half flung pie; Jan 10th, 2007 at 08:38 PM.

    Base 2
    Fcnncu"Nqxgu"Lguug##

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