Some strange corruption. Works in debug and in release mode within Does not run outside of

Hi experts,
I wrote a little programm which extracts some data from an .rtf file.
It works in debug mode and it also works in release mode when excecuted from within
It crashes, however, if I let it run outside of

You can download the project as a zip file here: All the source files are in there.
The error I get is something with strchr but I think I corrupted the heap somewhere.
An example file to process is also included.

I would need some advise on how to prevent things like this from happening in the future.

Please have a look,
Who is Participating?
waysideConnect With a Mentor Commented:
I have found your bug.

The problem is with your  CQuery::CQuery(CQuery &rValue) constructor and your Query &CQuery::operator=(CQuery &rValue) operator overload.

The original code (which is virtually identical for both) looks like this:

CQuery::CQuery(CQuery &rValue) {
  if(rValue.dtaFileName != NULL) {
    dtaFileName = new char[strlen(rValue.dtaFileName)+1];
  } else
    dtaFileName = NULL;
  initialized = rValue.initialized;
  mass = rValue.mass;
  if(sequence != NULL) {  //  <===!!! PROBLEM IS HERE !!!===>
    sequence = new char[strlen(rValue.sequence)+1];
  } else
    sequence = NULL;

The line marked above needs to be

  if(rValue.sequence != NULL) {

The problem is that you are testing the value of the new, uninitialized object, *not* the one you are copying from.

In debug this will work all the time because the memory is initialized to 0xcdcdcdcd for you, so the test always passes and you allocate memory.

In release mode, it works most of the time, because the random chunk of memory you get for the new object will have a non-null value, and memory will be allocated.

Every once in a while, though, the value will be NULL, so you don't allocate the memory, and it crashes further down the road.

allmerAuthor Commented:
Also works if I start the debug version outside of
If you build id in release mode with DBG info will it crash?
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

allmerAuthor Commented:
Hi migel,
I am not sure I am following you here.
If I build a release version and run it it will eventually crush at one point or another.
What is DBG info?
To add debug info into the release build you have to create new project configuration:
1.Choose Build|Configuration manager
select <New...> in the "Active solution configuration" combo
type Release DBG in the Solution configuration Name edit
choose Release in the Copy Settings from combo
in the project options choose
Release DBG in the configuration combo
select Programm database int the Configuration Properties|C/C++|General-Debug information  format
select Yes int the Configuration Properties|Linker|Debugging-Generate Debug Info
After tha your program release build will have debug info so debugger can show your crash point in the source code/
Now if your programm crash Windows will attach Just in time debugger so you can watch what happens
allmerAuthor Commented:
I did what you told me above but I think I missed something because now I get two unresolved symbols:

QueryCreator error LNK2019: unresolved external symbol __beginthreadex referenced in function "public: int __thiscall CWinThread::CreateThread(unsigned long,unsigned int,struct _SECURITY_ATTRIBUTES *)" (?CreateThread@CWinThread@@QAEHKIPAU_SECURITY_ATTRIBUTES@@@Z)

QueryCreator error LNK2019: unresolved external symbol __endthreadex referenced in function "void __stdcall AfxEndThread(unsigned int,int)" (?AfxEndThread@@YGXIH@Z)

So the programm won't compile.
I looked through the former questions and I could get rid of a third unresolved symbol but these to persist.
allmerAuthor Commented:
I am also having another problem, which could be involved in this specific point:
If I do what you set above, the
#include "stdafx.h" has to be changed to the following: //#include "../querycreator/stdafx.h"
since the files queries.cpp and query.cpp are located in a directory called shared whereas
the application is in the folder querycreator.
allmerAuthor Commented:
Hi Miguel we are almost done here.
Please tell me how to get this release debugging to run.
Can you just run this build under debugger? Is debugger stoppped when crash occured?
allmerAuthor Commented:
Hi Migel,
no crash occurs while running in debugger.
That is the problem I have.
when you run this program outside VS NET is it crash? Is system prompts you about debug crashed program?
allmerAuthor Commented:
It just crashes and wants to send information to Microsoft about it.
No debugging possible at that point.
But that is not important anymore because I rewrote the whole thing and it is running, now.
I would like your Release DBG to get running ;-)
If that works I can hopefully spot all the problems .. at least most of them after release.
Regards Jens
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.

All Courses

From novice to tech pro — start learning today.