-1

I am working on a ApxTrace class written by an ex-colleague

External class will call copyTrace() public member function to duplicate a ApxTrace class. ApxTrace class contains QVector data member

Here is the code:

void ApxTrace::copyTrace(ApxTrace& trace)
{
    *this = trace;
}

However it gave unhandled exception (QTCored4.dll): Access violation writing location 0xfeeeefeee in the debug version of the software. Interestingly to note is that code is working well and properly in the release version of the software.

The callStack shows that ApxTrace::operator=(const APxTrace &_that) is called, however, the ApxTrace class does not support assignment operator.

Please advice:

  1. Why the assignment operator is called in this case ?
  2. Comment on the CopyTrace() above ? I understand self assignment is not handled here.
  3. Why it is working well in the release version of the software?
uchar
  • 2,552
  • 4
  • 29
  • 50
user3753452
  • 139
  • 4
  • 11
  • 5
    Um, your function assigns `trace` to `*this`. What do you expect to happen, if not calling the assignment operator? – T.C. Jun 18 '14 at 20:03
  • As to Q3, your code likely has undefined behavior somewhere that the compiler helpfully checked for you in your debug build. Undefined behavior includes seeming to work, crashing, or [getting you pregnant](http://stackoverflow.com/a/1553407/2756719). – T.C. Jun 18 '14 at 20:06
  • 1
    To answer "Why is it working well in the release version of the software" the answer is that buggy software behaves unpredictably. Fix the bug and the mystery will go away. – David Schwartz Jun 18 '14 at 20:30

1 Answers1

1

1.Why the assignment operator is called in this case ?

Because it is called explicitly in the body of the function

*this = trace;

If it was not defined explicitly then the compiler defined it implicitly provided that it is not defined as deleted by the compiler.

As for the third question then it is possible that the program has undefined behaviour.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335