?
Solved

Memory Leak??

Posted on 2004-08-17
5
Medium Priority
?
493 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
[X]
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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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