2

I have encountered a strange bug in my program which was solved by making a destructor non-inline:

File a.h

class A {
    ...
    inline virtual ~A(){}
};

File b.h

class B : public A {
    ...
    virtual ~B(); // Defined in cpp file
};

Client code file.cpp

#include "a.h"
#include "b.h"
...
auto x = std::make_unique<B>();

The call to make_unique throws a bad_alloc exception. When I make ~A() non-inline and ~B() override, it stops throwing. Does anyone have any idea why it works? Another solution I found was to make printouts with cout in A and B's destructors. I have tried to simplify the relatively complex code as much as possible, so I must have left some crucial information out. What I want to know is what could have caused this strange behavior? Is overriding inline destructors with non-inline destructors somehow ambiguous?

K-J
  • 548
  • 2
  • 16
  • 2
    *I have encountered a strange bug in my program which was solved by making a destructor non-inline:* -- Unless you can explain why a piece of code "fixes" the bug, consider this a non-fix. All you really did was move code around, and doing that changes the binary image of the program. Undefined behavior, if any, gets moved to a new location in the program, masking the bug. I suggest you put the code back the way it was, and actually fix the problem. – PaulMcKenzie Aug 29 '20 at 18:15
  • 1
    Does this answer your question? [std::unique\_ptr with an incomplete type won't compile](https://stackoverflow.com/questions/9954518/stdunique-ptr-with-an-incomplete-type-wont-compile) – Quimby Aug 29 '20 at 18:17
  • @PaulMcKenzie that is a good point, that's why I consult SO. : ) – K-J Aug 30 '20 at 21:24
  • @Quimby no, I wouldn't say that since by the time make_unique is called, the destructor of B is known. I updated the question, I seem to get the same error when instantiating a local unique_ptr. – K-J Aug 30 '20 at 21:28

0 Answers0