Solved Screen Capture Memory Leak

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

user123

Newbie
Full Member
Jul 8, 2014
17
142
0
I use this piece of code to get pixel data from the screen, but each time I call it it adds ALOT of RAM (task manager> performance).
I am quite new to C++, so I have not really much of an idea how to fix it.
I only know to delete stuff you use with pointers, but thats about it.
P.S. Yes I know I should use something else to get a single pixel, but still I want to know how to fix this.
Code:
C++:
vector<int> GetScreenData(int x, int y)
{
	int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
	int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
	if (x > nScreenWidth || y > nScreenHeight || x < 1 || y < 1)return vector<int> {-1, -1, -1};
	HWND hDesktopWnd = GetDesktopWindow();
	HDC hDesktopDC = GetDC(hDesktopWnd);
	HDC hCaptureDC = CreateCompatibleDC(hDesktopDC);
	HBITMAP hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight);
	SelectObject(hCaptureDC, hCaptureBitmap);

	BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0, 0, SRCCOPY | CAPTUREBLT);

	BITMAPINFO bmi = { 0 };
	bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
	bmi.bmiHeader.biWidth = nScreenWidth;
	bmi.bmiHeader.biHeight = nScreenHeight;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 32;
	bmi.bmiHeader.biCompression = BI_RGB;

	RGBQUAD *pPixels = new RGBQUAD[nScreenWidth * nScreenHeight];

	GetDIBits(
		hCaptureDC,
		hCaptureBitmap,
		0,
		nScreenHeight,
		pPixels,
		&bmi,
		DIB_RGB_COLORS
		);
	int p = (x*nScreenWidth) + (nScreenHeight - y);
	int r = pPixels[p].rgbRed;
	int g = pPixels[p].rgbGreen;
	int b = pPixels[p].rgbBlue;
	vector<int> out = { r, g, b };
	delete[] pPixels;
	return out;
}
 

brinkz

Coder
Meme Tier VIP
Sep 3, 2012
209
1,688
12
You need to delete the DC's with DeleteDC and the Bitmap with DeleteObject.
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods