Virtual inheritance destruction issue

O

oliver

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 }
 
V

Victor Bazarov

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
 
R

red floyd

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top