[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

Memory Leak??

I am very new C++.  I am writing an application using MS VC++ 6.0 on Windows XP and having a slow increaseing use of memory every time I run a certain protion of my application.  Part of this code is shown below.  It is how I am doing my montoring.  

CMemoryState begMem;
begMem.Checkpoint();
{
      if(_vSession->input())
      {
            IntermodalOptimizationController ioc((Session*)_vSession);
            ioc.process();  
      }
      _vSession->output();
}
CMemoryState endMem;
endMem.Checkpoint();
CMemoryState diff;
diff.Difference(begMem,endMem);
diff.DumpStatistics();
begMem.DumpAllObjectsSince();

After montoring in this fashion I have reduce the amount of unfreeed Normal Bocks to one.  The current ouput is shown below:

0 bytes in 0 Free Blocks.
24 bytes in 1 Normal Blocks.
36 bytes in 1 CRT Blocks.
0 bytes in 0 Ignore Blocks.
0 bytes in 0 Client Blocks.
Largest number used: 143 bytes.
Total allocations: 5663806 bytes.
Dumping objects ->
{2053925} normal block at 0x0D039DE0, 24 bytes long.
 Data: <  F  4%         > 20 1A 46 01 80 34 25 01 02 00 CD CD CD CD CD CD
Object dump complete.

Even though the output showns  a memory leak of 24 bytes my application increases in memory size by an average of .7 MB everytime this code is ran.  Am I porperly montoring the memory?  Is there other ways I could be casuing leaks?   Any suggestion/help would be appreciated.    
0
nicoleheritage
Asked:
nicoleheritage
1 Solution
 
jkrCommented:
There could be a leak in 'process()', but without the code, this is hard to tell...
0
 
rstaveleyCommented:
There's not much to see here, but I'm wary of a constructor which requires a C-style cast to work.

i.e.

   IntermodalOptimizationController ioc((Session*)_vSession);

If you can't get it to work with a C++ static_cast<>, you should be suspicious about it.

i.e.

   IntermodalOptimizationController ioc(static_cast<Session*>(_vSession));
0
 
itsmeandnobodyelseCommented:
>> If you can't get it to work with a C++ static_cast<>

Nope, the constructor has to take _vSession - whatever it is.

Check for any 'new' or 'malloc' in your code or post the code here in EE.

Regards, Alex

0
 
nicoleheritageAuthor Commented:
I can get the static cast work.  Thanks for the advice.

I can not paste my code here cause it is to big and has multiple files.  I do not use “malloc” but I do use “new”.  I checked my new statements by looking at the output from "DumpStatistics" of the CMemoryState object.  Before I checked the new statements the number of object created, or normal blocks was huge, in the output from the DumpStatistics method.  Then I wrote code to clean up all my objects created by the new statements.  Now I have only one normal block that was not destroyed but my memory is still increasing, more than the 24 bytes that the DumpStatitics says.  I guess I have a couple questions.  

Am I using the CMemoryState properly?

Is the CMemoryState actually monitoring all the code that is performed in my process method?  I think it is cause if I create an object in the process method and do not delete it, it will add it to the number of normal blocks in the dump statistics.  

What other things besides new if any could cause memory leaks?    
0
 
itsmeandnobodyelseCommented:
>> What other things besides new if any could cause memory leaks?    

If you are using global class variables (e. g. with  CString members), the deletion of any member object occurs after endMem.CheckPoint() while the creation may happen when the object module has been called the first time. Also static class members may be responsible for the difference. If you are sure that each 'new' has an appropriate 'delete' you may not worry about that single leak.

BTW, if using VC6.0 compiler or younger you could use DEBUG_NEW macro that should display all 'new' statements that caused a memory leak. You have to add

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

at the top of any cpp file.

Regards, Alex



0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now