PDA

Click to See Complete Forum and Search --> : Free the memory


Chris
Nov 10th, 2000, 03:16 AM
How am i free out the allocate memory? My code just something show below.
When I monitor my memory usage with the System Task Manager, I notice that when I run this function it will increase the memory used by 4K. Why the delete function seem to be not working properly? It is my code miss out some important syntax?

10X thanks for reading my thread.


void Drawline(HWND hDlg)
{
int pCnt;
HPEN hPen;
HDC picHDC;
POINT *pt = new POINT[1024];

for(pCnt=0; pCnt < 1025; pCnt++)
{
pt[pCnt].x = pCnt;
pt[pCnt].y = pCnt;
}

hPen = CreatePen (PS_SOLID, 1, RGB(255,0,0));
picHDC = GetDC(hDlg);
SelectObject(picHDC, hPen);

Polyline(picHDC, pt, 1024);
UpdateWindow(hDlg);

DeleteObject(hPen);
DeleteDC(picHDC);
delete [] pt;
return;
}




[Edited by Chris on 11-10-2000 at 04:19 AM]

HarryW
Nov 10th, 2000, 03:27 AM
I have a feeling you're meant to use ReleaseDC(picHDC) instead of DeleteDC(picHDC).

Chris
Nov 10th, 2000, 03:41 AM
Hi HarryW, Thanks for your fast response. And I just check with MSDN and you're right. Below is the remarks that I found and would like to share with others that may facing the same problem as I facing.


The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common DC.

An application cannot use the ReleaseDC function to release a DC that was created by calling the CreateDC function; instead, it must use the DeleteDC function.

HarryW
Nov 10th, 2000, 03:59 AM
Hoorah! (It's not often that happens) :rolleyes:

Chris
Nov 10th, 2000, 04:19 AM
Hi HarryW, I Just run my program and I notice the problem still persits and the memory still does not release but keep on increasing (if I continue execute this function).
Below is the rectified code:
So, I suspect the pt array does not really free the memory with delete [] pt syntax. Do you agree with my opinion?
Do you have any good suggestion?
100x thanks.


void Drawline(HWND hDlg)
{
int pCnt;
HPEN hPen;
HDC picHDC;
POINT *pt = new POINT[1024];

for(pCnt=0; pCnt < 1025; pCnt++)
{
pt[pCnt].x = pCnt;
pt[pCnt].y = pCnt;
}

hPen = CreatePen (PS_SOLID, 1, RGB(255,0,0));
picHDC = GetDC(hDlg);
SelectObject(picHDC, hPen);

Polyline(picHDC, pt, 1024);
UpdateWindow(hDlg);

DeleteObject(hPen);
ReleaseDC(hDlg, picHDC);
delete [] pt;
return;
}



[Edited by Chris on 11-10-2000 at 05:22 AM]

HarryW
Nov 10th, 2000, 04:29 AM
Hmm... well I'm still thinking about it, but as a first off-the-top-of-my-head thought perhaps you ought to select the pen out of the DC before you delete them both. I doubt it matters though. Still thinking...

HarryW
Nov 10th, 2000, 04:43 AM
Hmm, one thing: your loop is looping 1025 times instead of 1024 (it starts at zero). I'm not sure if this makes any difference to memory allocation but it looks like you're accessing memory that isn't 'yours'. Perhaps this is leading to errors in your ReleaseDC and/or delete [] statements. Try checking return values for success, maybe something's going wrong. Other than the loop the code seems perfectly logical.

parksie
Nov 10th, 2000, 01:10 PM
When you use SelectObject, it returns the object that was selected out, therefore:

HPEN hNew;
HPEN hOld;
HDC hDC;

hOld = SelectObject(hDC, hNew);

// Use it

SelectObject(hDC, hOld);
DeleteObject(hNew);

That's the basics. You're supposed to leave the DC as it was in order to keep everything running smoothly.

wey97
Nov 10th, 2000, 03:12 PM
When I run this code, why is it that on the last cout, the 0 and 1 value have been erased, but 2-9 still print properly. The pointer is supposed to be undefined, so why does it still point to 2-9 values?


void main(void){

int *p = new int[10];
for (int i = 0; i < 10; i++){
*(p+i) = i;
cout << *(p+i) << endl;}

cout << endl;
delete [] p;

for (int j = 0; j < 10; j++)
cout << *(p+j) << endl;

}


??

parksie
Nov 10th, 2000, 03:26 PM
Perhaps the memory's been freed but not overwritten?

wey97
Nov 10th, 2000, 03:37 PM
That could be the case.
However, after the delete, shouldn't the pointer just point to garbage on the last eight values like it does for the first two values.
Run the code and see what you get.

parksie
Nov 10th, 2000, 04:26 PM
Mine prints out:

0
1
2
3
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

...so obviously the pointer is invalid, but the data's there. Definitely weird.

HarryW
Nov 11th, 2000, 07:41 AM
Surely that's not so weird; just because the memory's freed doesn't mean it's used yet. Or perhaps I'm missing some vital point.

Parksie: I know that you should select the old pen back in, but what could that have to do with the memory allocation?

Chris
Nov 12th, 2000, 09:09 PM
I delete the old pen is because my pen color will change on time I loop this function.
example I loop 7 times and my pen color will be something like:

1st times pen color = red
2nd times pen coloe = blue
3rd times pen coloe = green
4th times pen coloe = yellow
5th times pen coloe = black
6th times pen coloe = white
7th times pen coloe = grey

thanks for you reply
Chris.C

Originally posted by parksie
When you use SelectObject, it returns the object that was selected out, therefore:

HPEN hNew;
HPEN hOld;
HDC hDC;

hOld = SelectObject(hDC, hNew);

// Use it

SelectObject(hDC, hOld);
DeleteObject(hNew);

That's the basics. You're supposed to leave the DC as it was in order to keep everything running smoothly.

Chris
Nov 12th, 2000, 09:46 PM
Thanks HarryW, you're rite I did loop extra 1 element in my function and this cause the memory increase everytime I run this function. So I resolve my problem with your suggestion.

but, I found another two strange things:
My routine will used up 12K-16K memory, even though I have deleted the pt structure.
When I miminized the form where my function is locate, I found that the memory used is drop very much till just 70+K and increase back to 300+K (when my form is restore to the original position.). Which also much much less than the memory when the form first time loaded (1188K). Why?

Thanks for everyone reply, I really need to resolve this. Becauase my entire program is a graphic application which need to draw a lot of map.

Chris.C

Originally posted by HarryW
Hmm, one thing: your loop is looping 1025 times instead of 1024 (it starts at zero). I'm not sure if this makes any difference to memory allocation but it looks like you're accessing memory that isn't 'yours'. Perhaps this is leading to errors in your ReleaseDC and/or delete [] statements. Try checking return values for success, maybe something's going wrong. Other than the loop the code seems perfectly logical.

Chris
Nov 13th, 2000, 04:28 AM
Hi everybody,
I just found another strange things with the delete function. When I execute the following code I'll hits the runtime error and cause my application terminate automatically.
My code just like below:

void uMem(HWND hDlg)
{
char xData[128];
char *Ptr1 = new char[128];
char *Ptr2 = new char[128];
char *Ptr3 = new char[128];

strcpy(Ptr1, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_");
Ptr2 = strstr(Ptr1, "A");
Ptr3 = strstr(Ptr1, "#");
memset(xData,0,sizeof(xData));
strncpy(xData, Ptr1,Ptr3 - Ptr2);

delete Ptr1;
delete Ptr2;
delete Ptr3;
return;
}


It is my code not properly applied the delete function?

regards,
Chris.C

parksie
Nov 13th, 2000, 02:26 PM
I think strstr returns a pointer into the original string of the first occurence of that character. Therefore you need to use strstr and strncpy to get the required effect. What you're doing is reassigning the pointer, so they all end up pointing round in circles and that's why you get an error.

Chris
Nov 13th, 2000, 08:39 PM
I think everybody should know in VB we can have a dynamic array by using the ReDim Preserve syntax. But how this can be implement in VC++ (without MFC). Also, in VB we have something call DoEvents, so how about in VC++?


Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private pt() As POINTAPI
Private Sub dArray(ByVal MaxSize As Integer)
Dim p As Integer
Dim a As Integer
For p = 0 To MaxSize
ReDim Preserve pt(p)
pt(p).x = p
pt(p).y = p
DoEvents
Next
End Sub
Private Sub Command1_Click()
dArray 1024
End Sub


regards,
Chris.C

HarryW
Nov 14th, 2000, 07:02 PM
I think (that is, I think, I haven't ever checked) that what a DoEvents statement does is just check the application's message queue, and then dispatch any messages that are waiting. Since you seem to know what you're doing with Windows GDI graphics I think it's safe to assume you know how to use PeekMessage() or GetMessage() and then TranslateMessage() and DispatchMessage().

If I was going to resize an array then I would first make sure that the original was created using dynamic memory allocation, and then I'd have a funtion to copy the array contents to a new dynamically created array of the required size, free the old array and then move the pointer to point to the new array. I'd return this new pointer as the return value for the function. Does that sound reasonable?

As to your problem with delete, try using delete [] Ptr, because you're deleting an array.

Hope that helps some :)