Results 1 to 13 of 13

Thread: Is there a memory leak in my code?

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    761

    Is there a memory leak in my code?

    Hello!

    I have some code to take a screenshot of the bitmap.
    After like 300 runs, the VB6 IDE says good-bye with some graphic error. I know this behaviour from memory leaks.

    I have looked through my screencode-code again and again, but I don't see any memory leak.

    Can somebody tell me if there indeed isn't any leak? At the end of the code I do something else (using LaVolpe's c32bppDIB class, but I don't think that it doesn't have any errors, so I will leave this out for a start).

    Thank you!

    Code:
    Option Explicit
    
    Public c As c32bppDIB
    
    Private hDCSrc As Long
    Private hDCMemory As Long
    Private hBmp As Long
    Private hBmpPrev As Long
    
    Private m_bCleant As Boolean
    
    Public Sub TakeScreenshot()
    
        Dim WndHandle&
        WndHandle = GetDesktopWindow
    
        'Get Window Size
        Dim WidthSrc As Long
        Dim HeightSrc As Long
        Dim rc As RECT
        GetWindowRect WndHandle, rc
        WidthSrc = rc.Right - rc.Left
        HeightSrc = rc.Bottom - rc.Top
        
        'Get Window  device context
        hDCSrc = GetWindowDC(WndHandle)
        
        'create a memory device context
        hDCMemory = CreateCompatibleDC(hDCSrc)
        
        'create a bitmap compatible with window hdc
        hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
        
        'copy newly created bitmap into memory device context
        hBmpPrev = SelectObject(hDCMemory, hBmp)
    
        'copy window window hdc to memory hdc
        Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, 0, 0, vbSrcCopy)
          
        'Get Bmp from memory Dc
        hBmp = SelectObject(hDCMemory, hBmpPrev)
    
        'copy window window hdc to memory hdc
        Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, 0, 0, vbSrcCopy)
          
        Set c = New c32bppDIB
    
        Dim b As Boolean
        b = c.LoadPicture_ByHandle(hBmp)
        Debug.Assert b
        
    End Sub
    Public Sub CleanUp()
    
        hBmp = SelectObject(hDCMemory, hBmpPrev)
        DeleteObject hBmp
        
        DeleteDC hDCMemory
        
        Set c = Nothing
        
        m_bCleant = True
    
    End Sub
    Private Sub Class_Terminate()
    
        If Not m_bCleant Then
            Me.CleanUp
        End If
    
    End Sub
    And this is how I call it:

    Code:
    Private Sub btnTest_Click()
    
        Dim l&
        For l = 1 To 50
    
            Dim cSS As clsScreenshot
            Set cSS = New clsScreenshot
            cSS.TakeScreenshot
            cSS.c.SaveToFile_PNG "d:\blatest1.png", False
            cSS.CleanUp
            Set c = Nothing
            
        Next
    
    End Sub
    Last edited by tmighty2; Dec 17th, 2018 at 08:03 AM. Reason: I have included just everything now.

Tags for this Thread

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