Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual inheritance destruction issue

Reply
Thread Tools

Virtual inheritance destruction issue

 
 
oliver
Guest
Posts: n/a
 
      07-14-2011
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 }
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-14-2011
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      07-14-2011
On Jul 14, 7:31*am, oliver <(E-Mail Removed)> 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual base class and construction/destruction order BeautifulMind C++ 7 02-08-2007 12:35 PM
Object Destruction with / without Virtual Function V Patel C++ 5 01-30-2007 06:50 AM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Re: virtual destruction when? Andrey Tarasevich C++ 4 08-26-2003 05:13 PM
Re: virtual destruction when? Andrew Koenig C++ 1 08-23-2003 07:09 AM



Advertisments