Hi,

my console application works well if debug settings are used. Even if I
build a release version and run it in the IDE everything is fine. If I
execute the release version of the file, however, it crashes.

If I trace the error using the debugger, it marks the following lines:

...
struct Value
{
int var;
int mv;
};

...
vector<Value>::iterator iter_test;
vector<Value>::iterator iter_cur;
...
vector<Value> vTestVar;
vector<Value> vCurVar;

iter_cur = vCurVar.begin(); iter_test = vTestVar.begin();
...
// this section of the code tests if
// vector A (vTestVar) is equal to vector B (vCurVar)
if ((*iter_test).var == (*iter_cur).var) // same?
{
for(; iter_cur != vCurVar.end(); ++iter_cur)
{
iter_test = vTestVar.begin();
// test if (*iter_cur).mv is member in iter_test
while ( ((*iter_test).mv != (*iter_cur).mv) && (iter_test !=
vTestVar.end()))
++iter_test;
...

Any suggestion why it crashes? Why is it running with debug settings?

Greetings,

Arno

Re: Debug version works but release crashes by Ulrich

Ulrich
Tue Oct 24 05:40:00 CDT 2006

Arno Kromke wrote:
> struct Value
> {
> int var;
> int mv;
> };
>
> ...
> vector<Value>::iterator iter_test;
> vector<Value>::iterator iter_cur;
> ...
> vector<Value> vTestVar;
> vector<Value> vCurVar;
>
> iter_cur = vCurVar.begin();
> iter_test = vTestVar.begin();
> ...
> // this section of the code tests if
> // vector A (vTestVar) is equal to vector B (vCurVar)
> if ((*iter_test).var == (*iter_cur).var) // same?

Okay, a few things here:
1. There is an overloaded operator== for vectors, provided the elements have
an overloaded operator==. Implement the latter and simply compare
vTestVar==vCurVar. Well, except of course if your comparison does anything
special. Hard to tell with hardly documented code that is further mangled
by the posting.
2. You never care for the case that either vector is empty.
3. '(*it).var' is equivalent to 'it->var', which is IMHO easier to read.

> Any suggestion why it crashes? Why is it running with debug settings?

Things that are different in debug mode:
- assert/ASSERT are evaluated (make sure those don't contain side-effects!).
- uninitialized vars are filled with a special value to be able to recognise
accesses to them
- timing differences due to different optimisations
- variables might receive additional padding so that array overflows can be
detected (sentinel bytes)
- the C++ standardlibrary of VC8 does additional checks

Typically, the reason is that you are making assumptions that are not backed
up by reality. Try to distill a minimal testcase and see if that gives any
insight, otherwise post it here.

Re: Debug version works but release crashes by Tom

Tom
Tue Oct 24 06:10:44 CDT 2006

Arno Kromke wrote:
> Hi,
>
> my console application works well if debug settings are used. Even if I
> build a release version and run it in the IDE everything is fine. If I
> execute the release version of the file, however, it crashes.
>
> If I trace the error using the debugger, it marks the following lines:
>
> ...
> struct Value
> {
> int var;
> int mv;
> };
>
> ...
> vector<Value>::iterator iter_test;
> vector<Value>::iterator iter_cur;
> ...
> vector<Value> vTestVar;
> vector<Value> vCurVar;
>
> iter_cur = vCurVar.begin(); iter_test = vTestVar.begin();
> ...
> // this section of the code tests if
> // vector A (vTestVar) is equal to vector B (vCurVar)
> if ((*iter_test).var == (*iter_cur).var) // same?

What if either iter_cur or iter_test are end iterators? That then has
undefined behaviour.

> {
> for(; iter_cur != vCurVar.end(); ++iter_cur)
> {
> iter_test = vTestVar.begin();
> // test if (*iter_cur).mv is member in iter_test
> while ( ((*iter_test).mv != (*iter_cur).mv) && (iter_test !=
> vTestVar.end()))
> ++iter_test;

In the above, you are dereferencing iter_test *before* you check whether
it is an end iterator. You need to reverse the order of your &&.

> Any suggestion why it crashes? Why is it running with debug settings?

The code is wrong, although it isn't necessarily the case that the
problems above are what causes the crash.

Tom

Re: Debug version works but release crashes by pj_hern

pj_hern
Tue Oct 24 06:12:40 CDT 2006


Arno Kromke wrote:
> Hi,
>
> my console application works well if debug settings are used. Even if I
> build a release version and run it in the IDE everything is fine. If I
> execute the release version of the file, however, it crashes.
>
> If I trace the error using the debugger, it marks the following lines:
>
> ...
> struct Value
> {
> int var;
> int mv;
> };

No constructor provided, how are Value's members initialized?
You have to be organized when you code, the following lines are where,
in main? nobody knows.

> ...
> vector<Value>::iterator iter_test;
> vector<Value>::iterator iter_cur;

What vector type are you using, a std::vector? If so, the above should
be written like so:

// iterator type
typedef std::vector<Value>::iterator ValueIter;
// instances of that type, initialized to the their beginning
ValueIter iter_cur = vCurVar.begin();
ValueIter iter_test = vTestVar.begin();

> ...
> vector<Value> vTestVar;
> vector<Value> vCurVar;

your vectors are empty !!!

>
> iter_cur = vCurVar.begin();
> iter_test = vTestVar.begin();

both iterators are pointing to an empty container.

> ...
> // this section of the code tests if
> // vector A (vTestVar) is equal to vector B (vCurVar)
> if ((*iter_test).var == (*iter_cur).var) // same?

undefined behaviour. Your containers are empty.

> {
> for(; iter_cur != vCurVar.end(); ++iter_cur)
> {
> iter_test = vTestVar.begin();
> // test if (*iter_cur).mv is member in iter_test
> while ( ((*iter_test).mv != (*iter_cur).mv) && (iter_test !=
> vTestVar.end()))
> ++iter_test;
> ...
>
> Any suggestion why it crashes? Why is it running with debug settings?
>
> Greetings,
>
> Arno

Try:

#include <iostream>
#include <string>
#include <vector>

struct Value
{
int var;
Value(int n) : var(n) { }
};

int main()
{
std::vector<Value> vtest;
vtest.push_back(0); // look at the ctor above, var = 0
vtest.push_back(1);
vtest.push_back(2);

// builds a vector of 3 elements, all with var = 2
std::vector<Value> vcur(3, 2);

typedef std::vector<Value>::iterator ValueIter; // a new type
// instances of that type, both set
// to the beginning of their respective containers
ValueIter iter_test = vtest.begin();
ValueIter iter_cur = vcur.begin();

size_t count(0);
for(iter_test; iter_test != vtest.end(); ++iter_test, ++count)
{
std::cout << "vtest[" << count << "] = ";
std::cout << (*iter_test).var << "\t";
std::cout << "vcur[" << count << "] = ";
std::cout << (*iter_cur).var << "\t";

std::string result =
((*iter_test).var == (*iter_cur).var) ? "true" : "false";
std::cout << "equality: " << result;
std::cout << std::endl;
}
return 0;
}

/*
vtest[0] = 0 vcur[0] = 2 equality: false
vtest[1] = 1 vcur[1] = 2 equality: false
vtest[2] = 2 vcur[2] = 2 equality: true
*/