在 Visual Basic 中使用 GetPixel/GetDC 发生内存泄漏 [英] Memory Leak using GetPixel/GetDC in Visual Basic

查看:63
本文介绍了在 Visual Basic 中使用 GetPixel/GetDC 发生内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计时器,可以检查屏幕上的 5 个点是否有颜色变化.我的程序监视电话系统应用程序并检查是否有来自 5 个按钮中的任何一个的新来电.我正在根据我发布的另一个问题使用以下代码.在 Visual Basic 中监视屏幕区域的特定颜色

I have a timer that among other things, checks 5 spots on the screen for a color change. My program monitors a phone system app and checks to see if there is a new incoming phone call from any of 5 buttons. I'm using the following code based on another question I had posted. Monitor an area of the screen for a certain color in Visual Basic

Private Function CheckforCall()
    Try
        Dim queue1 As Integer = GetPixel(GetDC(0), 40, 573)
        Dim queue2 As Integer = GetPixel(GetDC(0), 140, 573)
        Dim queue3 As Integer = GetPixel(GetDC(0), 240, 573)
        Dim queue4 As Integer = GetPixel(GetDC(0), 340, 573)
        Dim queue5 As Integer = GetPixel(GetDC(0), 440, 573)
        ReleaseDC(0)

    <code snipped - Checks to see if the pixel color matches and 
       returns true or false>

    Catch ex As Exception
        Return False
    End Try
End Function

使用此代码,GDI 对象在短时间内迅速飙升,引发 OutOfMemory 异常.我假设我没有正确释放 DC,但我似乎找不到任何其他方法来做到这一点.

Using this code, GDI Objects skyrockets very quickly and within short order, throws an OutOfMemory exception. I'm assuming I'm not releasing the DC properly, but I can't seem to find any other way to do it.

推荐答案

调用 GetDC(0) 一次,将其保存到一个变量中,并将该变量传递给 ReleaseDC:

Call GetDC(0) once, save it to a variable, and pass the variable to ReleaseDC:

Dim hDC As IntPtr = GetDC(0)
Try
    Dim queue1 As Integer = GetPixel(hDC, 40, 573)
    Dim queue2 As Integer = GetPixel(hDC, 140, 573)
    Dim queue3 As Integer = GetPixel(hDC, 240, 573)
    Dim queue4 As Integer = GetPixel(hDC, 340, 573)
    Dim queue5 As Integer = GetPixel(hDC, 440, 573)
    ...
Catch ex As Exception
    Return False
Finally
    ReleaseDC(0, hDC)
End Try

请注意,ReleaseDC 需要两个IntPtr 参数,hWndhDC.

Note that ReleaseDC takes two IntPtr arguments, hWnd and hDC.

这篇关于在 Visual Basic 中使用 GetPixel/GetDC 发生内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆