Results 1 to 19 of 19

Thread: Free the memory

  1. #1

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Angry

    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.

    Code:
    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]

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    I have a feeling you're meant to use ReleaseDC(picHDC) instead of DeleteDC(picHDC).
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile

    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.

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Hoorah! (It's not often that happens)
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    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.

    Code:
    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]

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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...
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    When you use SelectObject, it returns the object that was selected out, therefore:
    Code:
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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?

    Code:
       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;
    
    }
    ??

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Perhaps the memory's been freed but not overwritten?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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.


  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Mine prints out:
    Code:
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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?
    Harry.

    "From one thing, know ten thousand things."

  14. #14

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Unhappy

    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:
    Code:
    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.

  15. #15

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    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.

  16. #16

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Angry delete function

    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:
    Code:
    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

  17. #17
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18

    Thread Starter
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    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++?

    Code:
    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

  19. #19
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width