Solved

VS2008 C++ memory leaks and how to fix them

Posted on 2011-02-25
7
184 Views
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.
0
Comment
Question by:CPlusPlusMemory
7 Comments
 

Author Comment

by:CPlusPlusMemory
ID: 34981906
Same here..
0
 
LVL 32

Expert Comment

by:phoffric
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.
    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.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 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.

Sara

p.s.

is it managed or unmanaged c++ ?
0
 
LVL 12

Expert Comment

by:Gideon7
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);
_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
0
 

Expert Comment

by:JGARNEAU
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now