Question:

Help with c memory leak

by  |  earlier

0 LIKES UnLike

a program displays the blue element of RGB of the pixel under the cursor (if u take out the cout<<... line it doesn't leak anymore)

#define VERSION 0.5.0

#define _WIN32_WINNT 0x0501 //very important!

#include <windows.h>

#include <iostream>

using namespace std;

POINT pos;

HDC hnd;

struct point {int x; int y;};

int getR(){

POINT pos;

//pos.x=-1;

//pos.y=-1;

GetCursorPos(&pos);

hnd=GetDC(NULL);

COLORREF clr;

clr=GetPixel(hnd,pos.x,pos.y);

delete hnd;

delete &clr;

return (int)GetRValue(clr);

}

int getG(){

POINT pos;

//pos.x=-1;

//pos.y=-1;

GetCursorPos(&pos);

hnd=GetDC(NULL);

COLORREF clr;

clr=GetPixel(hnd,pos.x,pos.y);

delete hnd;

delete &clr;

return (int)GetGValue(clr);

}

byte getB(){

GetCursorPos(&pos);

return GetBValue(GetPixel(GetDC(NULL),pos.x,pos...

}

int main(){

while(true){

Sleep(10);

cout<<(int)getB()<<endl;

}

return 0;

}

 Tags:

   Report

1 ANSWERS


  1. The return in the getB() function calls GetDC() without releasing it or deleting it.

    byte getB(void)

    {

    GetCursorPos(&amp;pos);

    HDC tempDC;

    DWORD tempRGB=GetPixel(tempDC=GetDC(NULL),....

    delete tempDC; // or releaseDC

    return GetBValue(tempRGB);

    }

    Something like that. Good luck.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.