Improve company productivity with a Business Account.Sign Up

x
?
Solved

Memory Leak??

Posted on 2004-08-17
5
Medium Priority
?
509 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 375 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

595 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