Solved

program crashes in no-debug mode

Posted on 2003-12-06
6
531 Views
Last Modified: 2012-06-27
Hello,

I have an incredibly weird problem with a small console application I've been writing. It's basically a console application to create/extract an archive type I've created myself.
What happens is that everything works fine as long as single-threaded DEBUG is turned on at my Visual Studio .NET compiler options. When I set it to single-threaded (default in release configuration), the program crashes at seamingly random lines (it crashes just after a cout << "test" << boolvarhere; line, and if I remove that line it just crashes at the beginning of the next while loop) with an error about heap allocation.

To be precise, the error I get is:
Unhandled exception at 0x77f485c0 in KARTool.exe: 0xC0000005: Access violation writing location 0x454e2e20.
The debugger points at line "return HeapAlloc(_crtheap, 0, size);" in malloc.c.

Also, the program only seems to crash if I enter a string of exactly 7 characters in one of the cin calls a few lines earlier. If I enter a 8-char string or a 6-char string the program continues but I'm sure it will crash eventually. Right now I've commented about 80% of the whole program and it still crashes after a few lines.

I don't use threads or anything and I haven't included any platform-specific headers. I only used the C++ standard library and a few functions from stdio.h/string.h.

As said earlier: I can fix the problem by turning single-threaded debug on, but that substantially increases the size of my executable and I want to get rid of this problem once and for all, not run from it. I had the same thing with one of my previous projects as well and I couldn't fix it back then either.


Thanks!
Karel Crombecq
0
Comment
Question by:Raveler
6 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 85 total points
ID: 9888404
This is very likely a problem with corrupted memory. Without seeing the source code it's pretty hard to pinpoint the exact source of the problem. Because you write that you are using functions from string.h, it's very likely that you are overwriting memory that you don't own. Which functions of string.h are you using, and how are you using them? You need to make sure that the memory that the functions expect to be allocated is actually there. Most of these functions do not allocate memory for you, you have to provide it before you call the function (e.g. strcpy expects that your target string is large enough to hold your source string).
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 90 total points
ID: 9888416
You might be able to narrow down the problem area by sprinkling a few calls to the following function
thruout your code:


void CheckHeap( char * Tag ) {
    int Len;
   fprintf( stderr, "Got to: %s\n", Tag );  
   for( Len = 10; Len < 10000; Len *= 2 ) free( malloc( Len ) );
}

It exercises the heap a bit and will bomb out if you've overrun a heap block somewhere.
It's found many a problem in my own code!


 
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9888469
Please show your code.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Raveler
ID: 9888701
You guys could have been right. Somewhere I found this piece of code:

fArchiveName = new char[strlen(archiveName)];
strcpy(fArchiveName, archiveName);

Guess I didn't leave any more room for the NULL terminating character. The program doesn't crash anymore but that doesn't mean anything because the crashes were very random. However I don't understand why it was crashing at the cout line then, as it doesn't have anything at all to do with the string that was copied (in fact, it wasn't used at all).
I'm gonna experiment some more and let you guys know.
0
 

Author Comment

by:Raveler
ID: 9888735
Okay, grg99's excellent debug function brought certainty. I managed to reproduce the crash. It was indeed the strcpy() function I mentioned earlier.
I guess the single threaded debug option has a few heap security checks that the non-debug version doesn't have.

I'm going to split the points evenly amongst kremer and grg (kremer for pointing me in the right direction and grg for providing a function that will surely help me a lot in the future).
0
 
LVL 22

Expert Comment

by:grg99
ID: 9889867
Here's a few more tips:

#define   sprintf   NoGoodRoutine::sprintf
#define   gets      NoGoodRoutine::gets
#define   strcpy   NoGoodRoutine::strcpy
#define   strcat   NoGoodRoutine::strcat

This wil help you catch all uses of these old and very dangerous functions.

All these functions should be replaced by their safer "sn" versions.



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

867 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

15 Experts available now in Live!

Get 1:1 Help Now