Learn how to a build a cloud-first strategyRegister Now


VS2008 C++ memory leaks and how to fix them

Posted on 2011-02-25
Medium Priority
Last Modified: 2015-06-21
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.
Question by:CPlusPlusMemory

Author Comment

ID: 34981906
Same here..
LVL 32

Expert Comment

ID: 34986253
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.
LVL 35

Accepted Solution

sarabande earned 2000 total points
ID: 34999231
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++ ?
LVL 12

Expert Comment

ID: 35765985
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

Expert Comment

ID: 36475669
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

810 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