• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

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.
1 Solution
CPlusPlusMemoryAuthor Commented:
Same here..
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.

Insure++ does much more than find memory leaks in real-time runs of your program.

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
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.
does the app crash both in debug and release? can you show pictures of the crash? can you go into the debugger when there is a crash?

generally access violation is always pointer related error and it is only hard to debug if a valid pointer was corrupted by some other not related action, for example by a writing beyond array boundaries or by using a pointer to local variable after scope has closed. in all other cases the crash should show you exactly where a pointer was null or invalid when analysing it in the debugger.

your 'relating to string overwriting' errors are typical errors when you have a class with string members and a pointer to an instance of the class which is null or invalid. then all normal string operations called for the members would crash because the internal pointers of string class are invalid as well.

memory leaks normally is a quite different problem and normally not related to access violations.



is it managed or unmanaged c++ ?
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);

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.
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

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now