|
-
November 16th, 2005, 09:26 AM
#1
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
-
November 16th, 2005, 11:58 AM
#2
Re: Memory Freeing Problem
-
November 16th, 2005, 12:11 PM
#3
Re: Memory Freeing Problem
 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.
 Originally Posted by raed_hasan
::CreateStreamOnHGlobal(NULL,TRUE,&pStream);
Check return values for success.
 Originally Posted by raed_hasan
the memory is keep increase after each call and nothing is freed,
Where did you see this memory "increase"?
 Originally Posted by raed_hasan
pStream->Release();
If you use the smart pointer, you comment this line out.
 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:
 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.
-
November 17th, 2005, 03:30 AM
#4
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 ...
-
November 17th, 2005, 04:35 AM
#5
Re: Memory Freeing Problem
 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.
-
November 17th, 2005, 09:49 AM
#6
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?
-
November 17th, 2005, 11:03 AM
#7
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.
-
November 17th, 2005, 11:51 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|