Problem freeing memory

Hi, i wrote a function that captures RGB of the desktop to memory, and this functions sets the new values through reference. But since this functions is called multiple times the programs memory size is increasing and after like 20 minutes it crashes, reached 20mb of ram usage.
How can i free the memory after the new values are set?
As i understand the bitPointer holds all the values? I tried deleting it but then i had access violation.
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
51
52
53
54
void Get_Color(int x,int y,int w,int h,int &red,int &green,int &blue,int action)
{
		HDC hdc, hdcTemp;
		RECT rect;
		BYTE* bitPointer;
		bitPointer=new BYTE[4*h*w];
		HWND Desktop = GetDesktopWindow();
		hdc = GetDC(Desktop);
		GetWindowRect(Desktop, &rect);
		hdcTemp = CreateCompatibleDC(hdc);
		BITMAPINFO bitmap;
		bitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
		bitmap.bmiHeader.biWidth = w;
		bitmap.bmiHeader.biHeight = h;
		bitmap.bmiHeader.biPlanes = 1;
		bitmap.bmiHeader.biBitCount = 32;
		bitmap.bmiHeader.biCompression = BI_RGB;
		bitmap.bmiHeader.biSizeImage = 0;
		bitmap.bmiHeader.biClrUsed = 0;
		bitmap.bmiHeader.biClrImportant = 0;
		HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
		SelectObject(hdcTemp, hBitmap2);
		BitBlt(hdcTemp, 0, 0, w, h, hdc, x, y, SRCCOPY);
		if(action==1)
		{
			for(int j=0;j<=w*h*4;j+=4)
			{
				red = (int)bitPointer[j+2];
				green = (int)bitPointer[j+1];
				blue = (int)bitPointer[j];
				if(red<30 && green>190 && blue>190)
				{
					break;
				}
			}	
		}
		else
		{
			for(int j=0;j<=w*h*4;j+=4)
			{
				red = (int)bitPointer[j+2];
				green = (int)bitPointer[j+1];
				blue = (int)bitPointer[j];
				break;
				
			}	
		}
		///RELEASE
		ReleaseDC(NULL,hdc);
		ReleaseDC(NULL,hdcTemp);
		
		

}
Last edited on
What do you do on the line 6 of your code?
Oh, okay, that is pretty uselss, it runs without it.. basically i allocated memory.
Okay, i need to reallocate bitpointer and delete it..
Topic archived. No new replies allowed.