0

I had a question on why my pointer ptr is having a core dumped error when its deleted.I'm currently testing the classes on GoogleTest.

I would not usually use multiple inheritance, but this class hierarchy was dictated by a class assignment.

For class E, the reason I called the constructor for C, was that if I left out it out, it would automatically resort to the default constructor of C, which is not what I want (Why is Default constructor called in virtual inheritance?). In my actual program, all the constructors have parameters, but I have left them out for simplicity. The simplified code below still throws the exact same error.

class A{
     public:
         virtual ~A(){}
};
class B: virtual public A{
     public:
         virtual ~B(){}
};
class C: virtual public B{
     public:
           virtual ~C(){}
};
class M: virtual public A{
     public:
          virtual ~M(){}
};
class D: virtual public C{
     public:
          D():C(){// calls constructor of class C

          }

          virtual ~D(){}
};
class E: virtual public M, virtual public D{
     public:
          E():M(),C(),D(){//calls constructors of class M, C, and D

          }
          ~E(){}
}

Within GoogleTest, if I run

TEST(filename,testE){
     A * ptr = new E();
     delete ptr; // free(): invalid pointer, Aborted (core dumped)
}

Thank you.

Stephen Wong
  • 177
  • 1
  • 8
  • Did you try running it locally? If yes, did you try debugging? – kesarling He-Him Mar 24 '21 at 07:24
  • works for me: https://godbolt.org/z/1Y4bT9fn5, please provide a [mre] – Alan Birtles Mar 24 '21 at 08:20
  • Interesting. Not sure if this is a quirk of GoogleTest. I tried it on my VScode compiler, and it worked fine as well. Let me see if I can create a minimal reproducible example by renaming variables, since I don't want to post the assignment code publicly – Stephen Wong Mar 24 '21 at 17:54
  • short : C++ allows you to shot a bullet in the foot. Using virtual inheritance and diamond is the highway to problem. Opinion base, but I would rather refactor things to not having to do such mess – sandwood Oct 23 '21 at 19:55

0 Answers0