My program is slowing down and my PC doesn't respond anymore

I was trying to create a program that automatically do that benchmark https://humanbenchmark.com/tests/reactiontime.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<windows.h>
#include<stdio.h>
#include <iostream>
using namespace std;

typedef WINAPI COLORREF (*GETPIXEL)(HDC, int, int);

int x=0, y=0;


int main(int argc, char** argv)
{
	Sleep(2000);
    HINSTANCE _hGDI = LoadLibrary("gdi32.dll");
    if(_hGDI)
    {
        while(true) {
            GETPIXEL pGetPixel = (GETPIXEL)GetProcAddress(_hGDI, "GetPixel");
            HDC _hdc = GetDC(NULL);
            if(_hdc)
            {
                POINT _cursor;
                GetCursorPos(&_cursor);
                COLORREF _color = (*pGetPixel) (_hdc, _cursor.x, _cursor.y);
                
                int _green = GetGValue(_color);
                int _blue = GetBValue(_color);

                
                if(_green==151 && _blue==80){
                	
                	Sleep(5);
                	
                	mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                	
                	Sleep(40);
			
			mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
			Sleep(50);
					
			}
                
                
            }
            FreeLibrary(_hGDI);
        }
    }
   
 return 0;
}


The program takes the RGB value of the current pointed pixel from the cursor. If it matches the RGB value of the green screen in the site it clicks it. The program works pretty well but after about 1 minute that it is running my PC slows down and become very hard to do anything, close the program too, do someone know why it is behaving like this?

Thanks
Last edited on
Well it's easier to see if your code is indented consistently, and not some pile of dog food.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<windows.h>
#include<stdio.h>
#include <iostream>
using namespace std;

typedef WINAPI COLORREF(*GETPIXEL) (HDC, int, int);

int x = 0, y = 0;


int main(int argc, char **argv)
{
  Sleep(2000);
  HINSTANCE _hGDI = LoadLibrary("gdi32.dll");
  if (_hGDI) {
    while (true) {
      GETPIXEL pGetPixel = (GETPIXEL) GetProcAddress(_hGDI, "GetPixel");
      HDC _hdc = GetDC(NULL);
      if (_hdc) {
        POINT _cursor;
        GetCursorPos(&_cursor);
        COLORREF _color = (*pGetPixel) (_hdc, _cursor.x, _cursor.y);
        int _green = GetGValue(_color);
        int _blue = GetBValue(_color);
        if (_green == 151 && _blue == 80) {
          Sleep(5);
          mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
          Sleep(40);
          mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
          Sleep(50);
        }
      }
      FreeLibrary(_hGDI);
    }
  }

  return 0;
}


Notice a couple of things.
1. FreeLibrary(_hGDI); is inside your while(true) loop, but you keep using it.

2. You keep calling GetDC, but you never bother to call ReleaseDC.
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdc
the above is the answer but to be explicit, your computer is showing the classic signs of running out of memory.
Registered users can post here. Sign in or register to post.