tags:

views:

145

answers:

3

EDIT following resolution, this has been substantially edited to dump irrelevant detail and explain the real issue.

I found a problem recently in some old code. The particular code was only rarely used and had insufficient unit tests (I was adding some more when I spotted the problem). I only relatively recently stopped using Visual C++ 2003, and symptoms didn't show in VC++9, only in MinGW GCC 4.4.0. And only release builds. And the symptoms vanished as soon as I added any trace code. Don't you just hate those?

Anyway, it turned out that the copy constructor and assignment operator overload weren't being called in all cases. As a result, a data structure was becoming inconsistent. Most of the time I was fluking through.

I spotted the issue by stepping through in the VC++9 debugger, even though the VC++9 build didn't show the symptoms. So, at least two compilers are behaving the same, even though the symptoms didn't show for one - a fair hint that the behaviour that caught me unawares is standards compliant, and I've been relying on old non-standard behaviour.

So... under what circumstances will the constructor etc for an inherited base class fail to be called?

FWIW, the problem code is part of the library that led to this question.

RESOLUTION

There were two separate issues - copy construction and overloaded assignment.

The copy construction issue turned out to be the well know return value optimisation issue, in the more recent "named" variant. The involved objects were "cursors" (like iterators) pointing into multiway tree leaf nodes. In order to ensure they are maintained, each leaf node keeps a linked list of cursors that refer into it.

The cursor is a very lightweight class - a leaf node pointer, a subscript within the node, and a next pointer for the maintenance list. The only reason that the explicit construction/destruction/assignment are needed is for the side-effect of maintaining the cursors lists.

Because the class is so lightweight, and because there are rarely more than a few cursors (and especially rarely more than one or two pointing into a particular leaf node), I sometimes used pass-by-value and return-by-value. The latter seems to be the problem.

Changing the copy constructor implementation accidentally bypassed this issue - I get the impression (unconfirmed) that for release builds, both MinGW GCC and VC++ still try to avoid bypassing copy constructors with side-effects, but miss some kinds of side-effects.

Second problem - the assignment overload. This, I think, was an old stupid mistake on my part, and if I hadn't had the copy constructor issue at roughly the same time, I'd have figured it out more quickly.

The assignment overload was inherited through two layers of inheritance, with some template, privacy and dependent-type complications along the way. Neither the middle nor derived class overrode it. This kind of makes sense (in an its-wrong-anyway way) in this special case - the derived class doesn't add any member data, it just supplies the template parameter to the base and adds some methods. But I'd be pretty surprised if the standard has special cases for "but i didn't add any member data".

With older compilers, I think I was fluking through on implicit casting rules allowing the base-class assignment operation to be called. I wouldn't like to try to trace the exact reason for the change in behaviour - the key point is that I'm being punished for writing some messy and bizarrely structured code.

+8  A: 

For a correct C++ implementation, this will never happen in correct code. If it does, you either have a compiler bug or your code has gone off into undefined behaviour land prior to the object construction because of bugs in your code. The latter is the most likely.

anon
This is very confusing - if true, why didn't I see some indication of the problem (other than the failure to call) while stepping through in the debugger? For example, there was no memory corruption. Quite simply, on "step into" the debugger simply stepped over. Repeated and disassembly checked - the generated code is the default default for POD types. And it still begs the question - what kind of undefined behaviour might cause this symptom?
Steve314
One possibly significant point - I spotted and checked the problem for the assignment overload. It's possible that I'm making assumptions with the constructors and destructors, though changing them certainly made bugs go away (though - nasty thought - perhaps just out of sight).
Steve314
@Steve Undefined behaviour (and compiler bugs) mean anything can happen. Once you are in UB land, reasoning about your application's behaviour is pointless. Perhaps posting the real code would help?
anon
@Neil - that's a *LOT* of code, if I can't narrow it down without changing the outcome. For undefined behaviour in general of course you're right by definition, but I named the "guilty" compilers - MinGW GCC 4.4.0 and Visual C++ 9. I can at least hope that someone else has experienced a similar issue and tracked it down better than I could. Of course I may have to write this off as the result of really nasty code that I was already planning to refactor before the bugs.
Steve314
Although I'm accepting MSalters answer, this and DeadMGs "never" answers were correct in a sense - I wasn't just failing to call the inherited-class constructor, but all copy constructors (including the derived class one), in a few specific RVO cases.
Steve314
+1  A: 

Never. If you create a Derived class, then the Base class constructor will always be called. If you invoke the Derived destructor (assuming that you remembered a virtual destructor), then the Base destructor will also be called. From the point of view of constructors/destructors, inheritance doesn't behave any different to composition. What you're likely seeing in the above is a form of ellision. Your compiler decided that you never do anything with that value, and got rid of it.

Edit: Or as Neil says, you found undefined behaviour first, and it caused this to happen.

DeadMG
+1  A: 

The most important case is when it skips calling copy constructors entirely. This is a specific optimization that is allowed even in cases where the observable behavior changes.

Another case that catches some inexperienced developers is that unless specified otherwise, Derived::Derived(T) calls Base::Base() - not Base::Base(T)

MSalters
After already changing the assignment operator to fix one issue, I changed both constructors and the destructor because I figured I was seeing a pattern - I was only sure of an issue with the copy constructor. The destructor is pretty tolerant since its only side-effect is a search-and-remove from a singly linked list with an end-of-list check out of habit. This skipped copy constructor issue could be a big part of my problem. Do you have a link to more detailed info. on the conditions? Is this just the return value optimisation? (I don't think that's relevant, but I'll double check).
Steve314
In general, when the compiler eliminates a copy, you'll indeed see one less copy constructor call, but also one less destructor call. When there's no objected created, it doesn't need to be destroyed either. You might want to check e.g. http://en.wikipedia.org/wiki/Copy_elision for details and more links.
MSalters
It *was* the return value optimisation. The assignment problem was different, and I'm not quite 100% on that, but I'll put the 99% confident answer in a question edit shortly.
Steve314