Results 1 to 3 of 3

Thread: Odd GetPixel Error - Memory overload - Program crash without an error

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    12

    Odd GetPixel Error - Memory overload - Program crash without an error

    Basicly I quickly wrote a simple version which translate a small icon in a numerical signature, the only problem is i can use it because after it is ran a couple of times my program crashes without an error; the window moves and becomes unusable(And in my taskmanager window I can see the memory increase by 100kb a time).
    I have tryed disabling all other functions in the loop and the only function that causes this problem is the function with GetPixel in it.

    I hope anybody with experience can solve my problem, here is the piece of code that causes the trouble:
    Code:
    Public Function analyseCard(ByVal hwnd As Long, ByVal realX As Long, realY As Long) As String
    Dim X As Long
    Dim Y As Long
    Dim cardValue As Long
        DoEvents
        realX = realX - 1
        realY = realY - 1
        For Y = realY To realY + 10
            For X = realX To realX + 9
                If GetPixel(GetDC(hwnd), X, Y) = vbWhite Then
                    DoEvents
                    cardValue = cardValue + (X - realX) + (Y - realY)
                End If
            Next
        Next
        
        Select Case cardValue
        Case 78
            analyseCard = "A"
        Case 52
            analyseCard = "B"
        Case 70
            analyseCard = "C"
        Case 72
            analyseCard = "D"
        Case 76
            analyseCard = "E"
        Case 8
            analyseCard = "F"
        Case 105
            analyseCard = "G"
        Case 73
            analyseCard = "H"
        Case 181
            analyseCard = "I"
        Case 69
            analyseCard = "J"
        Case 71
            analyseCard = "K"
        Case 43
            analyseCard = "L"
        Case 63
            analyseCard = "M"
        End Select
    End Function
    It looks alright to me though..

    Thanks,
    Pascal.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Odd GetPixel Error - Memory overload - Program crash without an error

    Welcome to the forums.

    Your routine is causing memory leaks.

    This part of your code is generating your error. Here's MSDN's official documentation and note remarks pertaining to ReleaseDC
    Code:
    GetDC(hwnd)
    Recommend two things
    1. Define new variable in routine: tgtDC As Long
    2. Modify your GetPixel call. A bit awkward due to your DoEvents within the loop
    Code:
        For Y = realY To realY + 10
            For X = realX To realX + 9
    
                If tgtDC = 0& Then tgtDC = GetDC(hWnd) ' << added
    
                If GetPixel(tgtDC, X, Y) = vbWhite Then ' << modified
                    ReleaseDC hWnd, tgtDC ' << added
                    tgtDC = 0& ' << added
                    DoEvents
                    cardValue = cardValue + (X - realX) + (Y - realY)
                End If
            Next
        Next
        If tgtDC Then ReleaseDC hWnd, tgtDC ' << added
    ....
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    12

    Re: Odd GetPixel Error - Memory overload - Program crash without an error

    Thank you very much, your first solution has worked:
    1. Define new variable in routine: tgtDC As Long

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