Link to home
Start Free TrialLog in
Avatar of CPlusPlusMemory
CPlusPlusMemory

asked on

VS2008 C++ memory leaks and how to fix them

My appln crashes with access violation error with error message relating to string overwriting etc.This has led me to start looking into memory leaks in my program. For this, I tried using various QA test packages, but have not had much success. The main reason is that with one of them the QA program itself crashes when performing memory checks, probably due to the memory overwriting. Another package I tried does not crash but is not really helpful.
I want to have brainstorm session with someone who has first hand experience of dealing with this kind of problems. For this, I can do a GoToMeeting demonstration session.
Avatar of CPlusPlusMemory
CPlusPlusMemory

ASKER

Same here..
Avatar of phoffric
A similar question was just raised today. So, I'll repeat the part especially for "access violation".

>> memory leaks
- the easiest of the memory issues to debug

>> access violation
- the hardest of the memory issues to debug

I used Parasoft C++ Insure++ program that literally savded a couple of projects. Given the quality of Insure++, and their staying power in industry, I recommend them. And they will give you a free trial with technical support which will help you greatly.
    http://www.parasoft-embedded.com/products/insure.jsp
    http://www.parasoft.com/

Insure++ does much more than find memory leaks in real-time runs of your program.
    http://www.parasoft.com/jsp/products/slideshow/flash.jsp?popup=true&subdir=/products/support/presentation/flash/insure/Presentation7&ver=2&title=Insure

It is the dynamic run-time detection of memory issues that is of importance. You run the program, and it spits out memory issues. If running a real-time application, then you can instrument just a piece at a time, or set up a simulation to allow for non-real-time dynamic checking. For details, give them a call.

Parasoft also has an extensive automated Unit Test and System Regression Testing Suite which you may also be interested in.


There is also Rationale Purify
     http://www-01.ibm.com/software/awdtools/purify/
which is a competitor of Insure++. It went out of favor at my company in the mid 90's when compared with Insure++; but since then, Purify was bought up by IBM, so perhaps, as part of the IBM "Rationale" line, it has improved.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Before spending lots of money on expensive C++ analysis tools, first try using the memory checking APIs that are already built in to Visual Studio.

To enable memory tracking:

int nOldState = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
_CrtSetDbgFlag(nOldState | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF);

This checks the entire memory heap for corruption on every malloc/free/new/delete operation.  It is slow but thorough.   Upon detecting corruption it reports the name of the offending source code file and the line number.

At the end of the program call _CrtDumpMemoryLeaks() to get a list of leaked memory locations and the source code line numbers where they were allocated.

You will need to perform a Debug build to gain access to the Crt functions.  For details see http://msdn.microsoft.com/en-us/library/1666sb98.aspx and http://msdn.microsoft.com/en-us/library/974tc9t1.aspx
The first step is to find the source of problem, in this case the Access Violation. Put a break (Debug->Exception->Win32 Exceptions->Access violation) and check the call stack.

Don't put leaks in your code!!! use smart pointer and std::string.