|
-
Nov 16th, 2009, 04:03 PM
#1
Thread Starter
New Member
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.
-
Nov 16th, 2009, 04:12 PM
#2
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
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
....
-
Nov 17th, 2009, 06:26 AM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|