4

Consider the following code:

class A 
{
public:
  A() {}
  ~A() {}
};

class B: public A
{
  B() {}
  ~B() {}
};

A* b = new B;
delete b; // undefined behaviour

My understanding is that the C++ standard says that deleting b is undefined behaviour - ie, anything could happen. But, in the real world, my experience is that ~A() is always invoked, and the memory is correctly freed.

if B introduces any class members with their own destructors, they won't get invoked, but I'm only interested in the simple kind of case above, where inheritance is used maybe to fix a bug in one class method for which source code is unavailable.

Obviously this isn't going to be what you want in non-trivial cases, but it is at least consistent. Are you aware of any C++ implementation where the above does NOT happen, for the code shown?

4
  • Undefined doesn't always mean unpredictable, in my experience. But in this case I don't understand the value - if you're using an A* pointer, none of the B methods will be invoked unless they're virtual methods in A to begin with. Dec 23, 2010 at 22:55
  • @Mark - true, but if I use a B* pointer, I think it's no longer undefined behaviour?
    – Roddy
    Dec 23, 2010 at 22:58
  • 1
    Correct, using a B* would eliminate all undefined behavior from this code. ~B gets called, followed by ~A, followed by freeing sizeof(B) memory. Dec 23, 2010 at 23:14
  • 1
    It may potentially blow up if there is multiple inheritance involved. Dec 24, 2010 at 1:55

4 Answers 4

6

This is a never-ending question in the C++ tag: "What is the predictable undefined behavior". Easy to resolve all by yourself: get every C++ compiler implementation and check if the predictable unpredictable still works. This is however something you have to do by yourself.

Do post back what you found out, that would be quite useful to know. As long as the unpredictable has consistent and annotated behavior across the board. It makes it really hard for somebody that writes a C++ compiler to get anybody to pay attention to his product. Standardization by convention, happens a lot with a language that has a lot of undefined behavior.

2
  • This is however something you have to do by yourself. :-) I was hoping to avoid that by asking here... Oh well!
    – Roddy
    Dec 23, 2010 at 23:10
  • Sounds easily parallelizable to me. I guess Hans really means, "some group of people not including me has to do it". The tricky part is putting together your list of "all C++ compilers", because you have to decide what to include and what to leave out. You want to rule out old rubbishy compilers like VC6, but you can't just say the obvious, "all conforming ones", because there are approximately no conforming C++ compilers. Besides, some people are still using old rubbishy ones. Sometimes people do straw polls at WG21 meetings. If nobody there cares about a compiler, then it doesn't matter. Dec 24, 2010 at 1:19
3

As far as I know, there's no implementation where that's not true. On the other hand, having a class where if you put a non-POD in it, it blows up, is the kind of bad thing that it's hardly unreasonable to describe as a bug.

In addition, your question title is incredibly misleading. Yes, it is a serious real world problem to not invoke a class's destructor. Yes, if you restrict the input set to an incredible minority of real world classes, it's not a problem.

7
  • 1
    You mean a minority of non-real world classes. In real world classes this sort of thing has no exceptions: it's a serious issue and it's real and it will bite later, if not now, when the derived class has virtual members or it manages its own resources. Dec 23, 2010 at 22:58
  • @wilhelmtell: Technically speaking, some classes are PODs and it's Standard-legal not to destruct them- there's a type trait for determining it.
    – Puppy
    Dec 23, 2010 at 23:01
  • Do you have a better title? My point is that compilers tend to often do what you asked (however stupid) rather than what you wanted, and that doesn't make their behaviour 'undefined'. Why isn't the behaviour for non-virtual destructors 'defined'?
    – Roddy
    Dec 23, 2010 at 23:04
  • 1
    @Roddy: Sure. How about, "Non-virtual destructors- if derived is POD". And it's not defined because it's so insane, if the Standard defined it, that it would probably downgrade the quality of C++. If you want someone to inherit from your class, you make a virtual destructor. Easy. If you add in corner-cases for 0.001%, then all the new people will get caught up in it.
    – Puppy
    Dec 23, 2010 at 23:21
  • 1
    @DeadMG: POD classes have no bases, so any leniency for PODs doesn't apply to B. Dec 24, 2010 at 1:12
1

Class B will almost always be larger than A in non-trivial cases, because it has an instance of A inside it as well as the additional members of B. It gets worse when you introduce virtual members.

So while ~A will get called, it is easy to see that this kind of thing can result in a memory leak. It is undefined behaviour not because it may not call ~A, that is basically guaranteed, but by how the memory is managed.

3
  • 1
    The memory manager is typically responsible for remembering how many bytes you asked for - (so that free() doesn't need a size passed in). Unless the additional members involved further dynamic allocations I don;t believe that would be a real world problem?
    – Roddy
    Dec 23, 2010 at 22:57
  • Why would there be a memory leak? Unless the B object got allocated with two separate blocks (one for the A portion and one for the remainder representing B, which is extremely unlikely, if not impossible), then there would be a single allocation, and after delete calls the (wrong) destructor, that single block would get freed. Where's the leak? Dec 23, 2010 at 23:00
  • 1
    Yes, this is what what happens in theory, but since you are telling the compiler that is an instance of A, the behavior is still undefined. Rely on UB if you must, but it may stop working for no apparent reason, and it may not be portable. Dec 23, 2010 at 23:03
1

"For the code shown" it is very unlikely you'll ever find an implementation that will act up. That is if we exclude from consideration various "debugging" implementations, which are specifically and intentionally designed to catch such errors.

1
  • A "debugging implementation", if by that you mean runtime instrumentation such as UBsan, isn't even necessary: g++ will warn about this at compile time if it can identify it. And it's not necessarily sufficient either... as the (admittedly rather old, 4.9.2 from Debian Jessie) version of UBsan I have in g++ on this secondary machine doesn't notice the UB deletion happening right in front of it. Point being: this is something that can, and must, be caught at compile time, as anything after that is too late. Of course, it must be only a warning, since if *base is a Base there's no UB Sep 27, 2016 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.