Memory Problems!!!
-
I hope someone can figure this one out for me! I have a simple function that takes 400 double values and graphs them in the client area. I run this about 25 times per second to give 25 FPS. This works fine however it uses up all the memory in my computer after about ten minutes!!! In the "Windows Task Manager" the mem usage just keeps increasing until my computer crashes. I'm sure it is this function that is causing the problem because when I leave it out this doesn't happen. Any ideas? void function3 (HWND hwnd, double a[], int iLength) { HDC hdc; PAINTSTRUCT ps; int i; hdc = GetDC(hwnd); Rectangle(hdc, 0, 0, 400, 256); MoveToEx(hdc, 0, 256, NULL); for (i=1; i< 400; i = i++) { int p = (int)a[i]; LineTo(hdc, i, 255-p); } EndPaint (hwnd, &ps) ; } Thanks in advance, Paddy
-
I hope someone can figure this one out for me! I have a simple function that takes 400 double values and graphs them in the client area. I run this about 25 times per second to give 25 FPS. This works fine however it uses up all the memory in my computer after about ten minutes!!! In the "Windows Task Manager" the mem usage just keeps increasing until my computer crashes. I'm sure it is this function that is causing the problem because when I leave it out this doesn't happen. Any ideas? void function3 (HWND hwnd, double a[], int iLength) { HDC hdc; PAINTSTRUCT ps; int i; hdc = GetDC(hwnd); Rectangle(hdc, 0, 0, 400, 256); MoveToEx(hdc, 0, 256, NULL); for (i=1; i< 400; i = i++) { int p = (int)a[i]; LineTo(hdc, i, 255-p); } EndPaint (hwnd, &ps) ; } Thanks in advance, Paddy
Paddy wrote: void function3 (HWND hwnd, double a[], int iLength) { How often do you call this fcn? the double a[] with no bounds checking seems kinda shady... If I can recall, every time you call this fcn, you will make a copy of a[]. I would get the for loop out of this fcn, and pass a as a const reference. Also, why even use a double if you're casting to int X| And also, where do you set the size of a[]? I'm not a guru, but the whole thing looks a little shady. - Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
-
I hope someone can figure this one out for me! I have a simple function that takes 400 double values and graphs them in the client area. I run this about 25 times per second to give 25 FPS. This works fine however it uses up all the memory in my computer after about ten minutes!!! In the "Windows Task Manager" the mem usage just keeps increasing until my computer crashes. I'm sure it is this function that is causing the problem because when I leave it out this doesn't happen. Any ideas? void function3 (HWND hwnd, double a[], int iLength) { HDC hdc; PAINTSTRUCT ps; int i; hdc = GetDC(hwnd); Rectangle(hdc, 0, 0, 400, 256); MoveToEx(hdc, 0, 256, NULL); for (i=1; i< 400; i = i++) { int p = (int)a[i]; LineTo(hdc, i, 255-p); } EndPaint (hwnd, &ps) ; } Thanks in advance, Paddy
Paddy wrote: hdc = GetDC(hwnd); You have to call ReleaseDC() if you've called GetDC() Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
-
Paddy wrote: hdc = GetDC(hwnd); You have to call ReleaseDC() if you've called GetDC() Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
-
Paddy wrote: void function3 (HWND hwnd, double a[], int iLength) { How often do you call this fcn? the double a[] with no bounds checking seems kinda shady... If I can recall, every time you call this fcn, you will make a copy of a[]. I would get the for loop out of this fcn, and pass a as a const reference. Also, why even use a double if you're casting to int X| And also, where do you set the size of a[]? I'm not a guru, but the whole thing looks a little shady. - Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
I'm calling this function about 25 times per second, I know the code is a bit of a mess but I'm just worried about the increasing memory problem. I have included ReleaseDC(hwnd, hdc); now and it has made a significant difference but I'm still losing a bit of memory! I can't actually get rid of the for loop because I'm using this to draw lines between each point on the graph. Thanks for the help.
-
I'm calling this function about 25 times per second, I know the code is a bit of a mess but I'm just worried about the increasing memory problem. I have included ReleaseDC(hwnd, hdc); now and it has made a significant difference but I'm still losing a bit of memory! I can't actually get rid of the for loop because I'm using this to draw lines between each point on the graph. Thanks for the help.
Try:
void function3 (HWND hwnd,
double& a[400]
, int iLength)
{
HDC hdc;
PAINTSTRUCT ps;
int i= 0
;hdc = GetDC(hwnd);
Rectangle(hdc, 0, 0, 400, 256);
MoveToEx(hdc, 0, 256, NULL);
for (i=1; i< 400;
i++
)// <-- why start @ i=1 and not 0??
{
//int p = (int)a[i];
LineTo(hdc, i, (255 - static_cast<int>(a[i]));
}EndPaint (hwnd, &ps) ;
}It may not solve all the memory issues, but may speed you up some and maybe ease it a little. - Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
-
Try:
void function3 (HWND hwnd,
double& a[400]
, int iLength)
{
HDC hdc;
PAINTSTRUCT ps;
int i= 0
;hdc = GetDC(hwnd);
Rectangle(hdc, 0, 0, 400, 256);
MoveToEx(hdc, 0, 256, NULL);
for (i=1; i< 400;
i++
)// <-- why start @ i=1 and not 0??
{
//int p = (int)a[i];
LineTo(hdc, i, (255 - static_cast<int>(a[i]));
}EndPaint (hwnd, &ps) ;
}It may not solve all the memory issues, but may speed you up some and maybe ease it a little. - Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
Thanks I have it fixed now, there was another part in my app where I was creating a colored pen over and over again using
hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
and not deleting it when I was finished. Just stuck inDeleteObject(hPen);
and it's all sorted now! Thanks for the help, Paddy -
Thanks I have it fixed now, there was another part in my app where I was creating a colored pen over and over again using
hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
and not deleting it when I was finished. Just stuck inDeleteObject(hPen);
and it's all sorted now! Thanks for the help, PaddyMy product MemWatcher[^] will help identify if you're leaking resources. /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com