Solved

Memory Leak??

Posted on 2004-08-17
5
462 Views
Last Modified: 2008-02-26
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
Comment
Question by:nicoleheritage
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 11822057
There could be a leak in 'process()', but without the code, this is hard to tell...
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 11822193
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 11823027
>> 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
 

Author Comment

by:nicoleheritage
ID: 11823358
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 125 total points
ID: 11823566
>> 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now