CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2004
    Posts
    25

    Memory Freeing Problem

    I'm creating a stream object stored in global memory.

    GetImage() function

    IStream *pStream;
    ::CreateStreamOnHGlobal(NULL,TRUE,&pStream);
    image->Save(pStream,&encoderClsid,&encoderParameters);
    ..// bit operations no memory allocations just math calcuations
    //then
    delete image;
    pStream->Release();

    the problem i have when application is run ,and begin executing the GetImage
    Function, the memory is keep increase after each call and nothing is freed,
    what should i do to free memory after each call the two statements :

    delete image;
    pStream->Release(); as they have to effects

    I also tried to use GlobalFree(pStream), and GlobalFree(&pStream) with no
    avail. memory keep rasing after each call

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Memory Freeing Problem

    Do you get memory leaks reports?

    How to manage memory leaks?
    Visual Leak Detector
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Memory Freeing Problem

    Quote Originally Posted by raed_hasan
    IStream *pStream;
    Please don't use raw interface pointers.

    This is better written as -
    Code:
    CComQIPtr <IStream> pStream;
    ...You will not have to worry about reference counting - the smart pointer will release for you when it goes out of scope.

    Quote Originally Posted by raed_hasan
    ::CreateStreamOnHGlobal(NULL,TRUE,&pStream);
    Check return values for success.
    Quote Originally Posted by raed_hasan
    the memory is keep increase after each call and nothing is freed,
    Where did you see this memory "increase"?
    Quote Originally Posted by raed_hasan
    pStream->Release();
    If you use the smart pointer, you comment this line out.

    Quote Originally Posted by raed_hasan
    I also tried to use GlobalFree(pStream), and GlobalFree(&pStream) with no avail. memory keep rasing after each call
    Naturally to no avail. GlobalFree accepts a HGLOBAL and not an interface pointer. You are lucky worse things didn't happen.

    Anyways, you don't need to do this.

    Reason:
    Quote Originally Posted by MSDN
    CreateStreamOnHGlobal -

    If the caller sets the fDeleteOnRelease parameter to FALSE, then the caller must also free the hGlobal after the final release. If the caller sets the fDeleteOnRelease parameter to TRUE, the final release will automatically free the hGlobal as shown in the following code excerpt.
    Last edited by Siddhartha; November 16th, 2005 at 12:14 PM.

  4. #4
    Join Date
    May 2004
    Posts
    25

    Re: Memory Freeing Problem

    I did some testing for my code i notice by using system task manager MEM Usage that this block

    1: CString strImage;
    2: GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

    3: SIZE sz;
    4: sz.cx = ::GetSystemMetrics(SM_CXSCREEN);
    5: sz.cy = ::GetSystemMetrics(SM_CYSCREEN);

    6: HDC hdc = ::GetDC(::GetDesktopWindow());
    7: Graphics *g1 = new Graphics(hdc);
    8: Image *image = new Bitmap(sz.cx,sz.cy,g1);//
    9: Graphics *g2 = Graphics::FromImage(image);

    10: delete image;

    before the line 8 the mem is (420616 KB) after call line 8 the mem be (425536 KB) , then after execute line 10 the mem is (425592 KB) , so after make serveral call the mem in Task Manager MEM Usage screen keep raising ...

  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Memory Freeing Problem

    Quote Originally Posted by raed_hasan
    before the line 8 the mem is (420616 KB) after call line 8 the mem be (425536 KB) , then after execute line 10 the mem is (425592 KB) , so after make serveral call the mem in Task Manager MEM Usage screen keep raising ...
    That is because the parameter you are observing in the Task Manager is not the right one to give you an indication of realtime memory consumption.

    Anyways, a better tool than Task Manager is -

    Also note that just because you have done a delete does not mean that the memory has to be released to the OS / System right away. It doesn't. The heap manager may choose to keep it for sake of later allocations.

    The best way to locate leaks is to use a utility like Bounds Checker that will even tell you the line of code in error.
    Last edited by Siddhartha; November 17th, 2005 at 04:43 AM.

  6. #6
    Join Date
    May 2004
    Posts
    25

    Re: Memory Freeing Problem

    that's ok, but i made a Release Version of application and install it in client machine
    it seems that memory will be full as they monitor the Task Manager MEM Usage,
    so they claimed that the application has memory problems which makes the
    machine slow due to mem usage, and I do not know how to handle this, so i
    , so i went there i checked the mem usage and machine performance after 5 mintues of application running ,a not enough virtual memory message is reported.

    The problem there is no Memory Leak report after end the application in visual studio....so what do u advice me to do?

  7. #7
    Join Date
    May 2002
    Location
    Phoenix, AZ
    Posts
    95

    Re: Memory Freeing Problem

    Try using UMDH and AppVerifier. These tools are available free of cost from Microsoft.

    Check out
    http://www.microsoft.com/whdc/devtoo...g/default.mspx
    and
    http://www.microsoft.com/technet/pro...pverifier.mspx

    I would suggest that you build your application in release mode with symbols (so that you get the PDBs). Also make sure that you have the symbols for whatever OS build that you have. If not you can set the symserver path to http://msdl.microsoft.com/download/symbols.

    P.S. When using AppVerifier on your application, you will need to have a debugger attached. I would suggest that you use NTSD as it has the least over head when comapred to WinDBG or Visual Studio, or whatever.

  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Memory Freeing Problem

    Use a utility like Bounds Checker. Order their evaluation version if available and you will be impressed.

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

Featured