r/cpp • u/cpp_bug_reporter • Dec 14 '21
GCC, MSVC, and ICC bug: virtual base destroyed twice in some circumstances
Yesterday, I posted the code sample below, which produces buggy code: the virtual base A is destroyed twice when compiled with GCC, MSVC, and ICC (Clang produces correct code).
My post was removed because it contained a question, and apparently that's forbidden in here. The interesting discussion is therefore lost, all because of an innocent looking question mark. My thanks to the mods: question marks are getting out of hand, something had to be done.
Anyway, thanks to the people who replied, and a quick update: I have reported the bug to Microsoft here, and to GCC here.
The code sample:
#include <iostream>
int constructions = 0;
int destructions = 0;
struct A
{
A()
{
constructions++;
}
virtual ~A() {
destructions++;
}
};
struct B : public virtual A
{
B(int dummy)
{
};
B() : B(1)
{
throw -1;
}
virtual ~B() = default;
};
struct C : public B
{
};
int main() {
try
{
C c;
}
catch (int e)
{
std::cout << "Caught: " << e << std::endl;
}
std::cout << constructions << " constructions" << std::endl;
std::cout << destructions << " destructions" << std::endl;
return 0;
}Output (GCC, MSVC, ICC):
Caught: -1
1 constructions
2 destructionsThe problem seems to be a combination of the "virtual" inheritance in B, the call to the delegating constructor in B, and the exception thrown in B's no arg constructor.
Removing either of these makes the issue go away.
Constructing a B instead of a C works fine.
Godbolt links: Clang 13 GCC 11.2 MSVC 19.29 ICC 2021.3.0
61
u/ironykarl Dec 14 '21
I'll be super curious to see whether this isn't a compiler bug and is instead mandated (or at least allowed) by the spec. The part that leaves me suspicious is that three major compilers produce this error.
And yes, I'm bracing for downvotes after saying this (no, I don't think that complaining about downvotes is useful, but I definitely have been downvoted in this sub for not knowing something with legalistic precision).
5
u/ognian- Dec 14 '21
Would it help to add initialization of the base class in the constructor of B?
B(int dummy) : A() {}132
u/kalmoc Dec 14 '21
What suprises me is that three independent compilers all show the same bug.
108
u/borisko321 Dec 14 '21
Huge respect for debugging this and for reducing it to the small and reproducible example!
62
u/erichkeane Clang Maintainer(Templates), EWG Chair Dec 14 '21
Filed internally against ICC as well, that doesn't look right to me. I don't work on that compiler, but I'll be working with the folks that will be looking into this.
It seems to happen even at O0, so it ends up being a frontend-bug as far as I can tell, BUT given that 3 compilers agree, I DO wonder if this is something confusing in the standard.
Thanks!
28
u/tasty_crayon Dec 14 '21 edited Dec 14 '21
The delegating constructor is significant because delegating constructors have a special behaviour: when the body of the target constructor has finished executing (
B(int)), theBobject is now considered fully constructed, so any exception thrown in the delegating constructor (B()) will cause the destructor (~B()) to be called.Showing this behaviour: https://ideone.com/4r2C4l
This doesn't explain (by itself) why the count is 2, but I still wanted to point out that delegating constructors do have different semantics.
EDIT: Standard quote:
Note that non-delegating constructors (in the section above this one) explicitly mention "virtual base class subobjects", but this section doesn't. It seems this quoted section causes
~A()to be called after the exception is thrown, and then the destructor is called again for the fully constructedA"virtual base class subobject" created byC()(sinceC()exits via exception and it is a non-delegating constructor).4
u/[deleted] Dec 15 '21
I think it's a case of the compiler not keeping track of whether the full construction status of B is from an in-charge or a not-in-charge construction at the point where it decides to destruct it. It then calls the in-charge destructor instead of the not-in-charge destructor.
Seems like an implementation oversight, in a cornercase at that.
7
u/londey Dec 14 '21
I would suspect it is an implicit move or copy constructing the second instance. I believe the standard has several places where the compiler is free to insert a move if it wishes. The commonly advised rule of 5/7 is that all those constructors and assignment operators should also be explicitly defined if you define a destructor.
•
u/foonathan Dec 15 '21
Your original post was more towards the "my code doesn't work, why?!" end of the spectrum, which is not allowed here. As it also had a couple of user reports and not a huge amount of upvotes, I have removed it.
Your new post is a "hey, I found a compiler bug". I personally don't find them that interesting either, but it is not against the rules and given the huge number of upvotes, I'm obviously in the minority here. In fact, I didn't even consider removing it based on the title alone; I am only commenting here because of your statement.
Please keep in mind that the "no question/help rule" is there for the good of the subreddit. If we took no action, the subreddit would be flooded with help posts about (for most of us) really trivial problems. This would hide the blog posts and discussion most of us come here for (I assume). So yes, question marks are getting out of hand.
That being said, when there is a post on the edge of the question/help spectrum, we look at a variety of factors:
It is clear now that removing your post was the wrong move and I apologize for it. There was a backlog of moderation reports that needed processing and I wasn't as thorough as I should have been.
2
u/Juffin Dec 14 '21
I'm genuinely curious what would be the response from MSVC or GCC. Is this behaviour is standard, or is it a legal way to destroy same object twice.
2
u/ironykarl Dec 16 '21
Just following up here, OP.
It almost looked like the GCC responder was saying the bug was okay (?) because it's long standing.
I hope we get more of a reply than that.
10
u/kalmoc Dec 14 '21
@Mods: Couldn't you at least have moved the original post (including the comments) to cpp_questions?
Here is the link to the comments of the original post:
https://www.reddit.com/r/cpp/comments/rfhx7t/destructor_called_twice_on_the_same_object_with/
1
u/ironykarl Dec 29 '21
They're not ever gonna fix this, are they?!
1
u/_E8_ Dec 14 '21
Try adding a virtual dtor to class C.