Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Memory Leak??

Posted on 2004-08-17
5
474 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

809 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