Virtual inheritance destruction issue

Discussion in 'C++' started by oliver, Jul 14, 2011.

  1. oliver

    oliver Guest

    My code is as follow. I know that I should use virtual destructor in
    A. Why the following code gave me the following complain.

    *** glibc detected *** ./private: free(): invalid pointer:
    0x0000000001a92018 ***

    The environment is Ubuntu 10.10 amd64 with gcc "gcc version 4.4.5
    (Ubuntu/Linaro 4.4.4-14ubuntu5)"

    1 using namespace std;
    2 #include <iostream>
    3
    4 class A
    5 {
    6 private:
    7 int a;
    8 };
    9
    10 class B : virtual public A
    11 {
    12
    13 };
    14
    15 int main()
    16 {
    17 A *ptr = new B;
    18 delete ptr;
    19 return 0;
    20 }
     
    oliver, Jul 14, 2011
    #1
    1. Advertising

  2. On 7/14/2011 10:31 AM, oliver wrote:
    > My code is as follow. I know that I should use virtual destructor in
    > A. Why the following code gave me the following complain.
    >
    > *** glibc detected *** ./private: free(): invalid pointer:
    > 0x0000000001a92018 ***
    >
    > The environment is Ubuntu 10.10 amd64 with gcc "gcc version 4.4.5
    > (Ubuntu/Linaro 4.4.4-14ubuntu5)"
    >
    > 1 using namespace std;
    > 2 #include<iostream>
    > 3
    > 4 class A
    > 5 {
    > 6 private:
    > 7 int a;
    > 8 };
    > 9
    > 10 class B : virtual public A
    > 11 {
    > 12
    > 13 };
    > 14
    > 15 int main()
    > 16 {
    > 17 A *ptr = new B;
    > 18 delete ptr;
    > 19 return 0;
    > 20 }


    And if you add a virtual d-tor to A, what happens?

    Victor
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 14, 2011
    #2
    1. Advertising

  3. oliver

    red floyd Guest

    On Jul 14, 7:31 am, oliver <> wrote:
    > My code is as follow. I know that I should use virtual destructor in
    > A. Why the following code gave me the following complain.
    >
    > *** glibc detected *** ./private: free(): invalid pointer:
    > 0x0000000001a92018 ***
    >
    > The environment is Ubuntu 10.10 amd64 with gcc "gcc version 4.4.5
    > (Ubuntu/Linaro 4.4.4-14ubuntu5)"
    >
    > 1 using namespace std;
    >   2 #include <iostream>
    >   3
    >   4 class A
    >   5 {
    >   6     private:
    >   7         int a;
    >   8 };
    >   9
    >  10 class B : virtual public A
    >  11 {
    >  12
    >  13 };
    >  14
    >  15 int main()
    >  16 {
    >  17     A *ptr = new B;
    >  18     delete ptr;
    >  19     return 0;
    >  20 }


    Isn't that UB? Deleteing an object with no virtual destructor
    through a pointer to the base class?
     
    red floyd, Jul 14, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Tarasevich

    Re: virtual destruction when?

    Andrey Tarasevich, Aug 22, 2003, in forum: C++
    Replies:
    4
    Views:
    549
    John L Fjellstad
    Aug 26, 2003
  2. Andrew Koenig

    Re: virtual destruction when?

    Andrew Koenig, Aug 22, 2003, in forum: C++
    Replies:
    1
    Views:
    416
    John Harrison
    Aug 23, 2003
  3. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  4. V Patel
    Replies:
    5
    Views:
    312
    Yahooooooooo
    Jan 30, 2007
  5. BeautifulMind
    Replies:
    7
    Views:
    649
    Ron Natalie
    Feb 8, 2007
Loading...

Share This Page