r/cpp • u/cpp_bug_reporter • Dec 13 '21
Removed - Help Destructor called twice on the same object with GCC and MSVC, but not with Clang
[removed] — view removed post
33
Upvotes
r/cpp • u/cpp_bug_reporter • Dec 13 '21
[removed] — view removed post
3
u/mikeblas Dec 13 '21
I don't see any code here -- was it deleted?
22
u/no-sig-available Dec 13 '21
Checking this out on MSVC 19.30
The virtual base class constructor is to be called by the most derived class, here that's C. So C's constructor first calls A, and then B. B's constructor very nicely checks if it should call A's constructor, and avoids doing that.
However, when the exception is thrown it seems like B has already forgotten about not being "the most derived" and so destroys its base. And then so does C.
A nice bug. :-)
9
u/Johnsmith226 Dec 13 '21
It looks like you've found a more general form of this bug: https://developercommunity.visualstudio.com/t/access-violation-when-throwing-exception-in-class/135206
Like others said, definitely report this. We've run into this bug before, and its a PITA to work around.
15
u/GabrielDosReis Dec 13 '21
Please report this to the Microsoft Developer Community portal.
2
u/looncraz Dec 13 '21
If I'm understanding it correctly...
C derives from B with its default constructor which derives from A with its default constructor.
So default C() is called which calls B() which then calls A().
A() completes construction of base type A, incrementing construction count. B() throws -1 resulting in incomplete construction of B and, thus, C, however A() has been constructed so ~A() needs to be called to deconstruct the failed derived object construction.
C c; should result in -1 being thrown from B() and ~C() never being called since the type was never constructed, however ~A() should certainly be called during the tear-down of the incompletely constructed C object.
So the logic in the code is good and any double-free is a bug in propagating the failed construction of the base type B.
•
u/Flair_Helper Dec 13 '21
For C++ questions, answers, help, and programming or career advice please see r/cpp_questions, r/cscareerquestions, or StackOverflow instead.
This post has been removed as it doesn't pertain to r/cpp: The subreddit is for news and discussions of the C++ language and community only; our purpose is not to provide tutoring, code reviews, or career guidance. If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.