473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Virtual destructor problem

Hi Guys,
Can someone please explain me why this is happening

#include<iostream>
using namespace std;

class a {

public:
int a1; // If I remove this it'll work fine
a() {
cout << "Constructor of a \n";
}
~a() {
cout << "Destructor of a \n";
}

};

class b : public a{

public:
b() {
cout << "constructor of b \n";
}
virtual ~b() { // If I remove virtual from here ... it'll work fine
cout << "destructor of b\n";
}

};

int main() {
a *p = new b;

delete p;

return 0;
}

Result is :
Constructor of a
constructor of b
Destructor of a
a.out(1563) malloc: *** error for object 0x100154: Non-aligned pointer
being freed
*** set a breakpoint in malloc_error_break to debug
----------------------------------------------------------------

I know that if I'll put virtual in front of the destructor of the base
class code will work fine. But, I want to use it the above way. Can
someone please explain me the reason behind this kind of behavior?

Thanks in advance
Jun 27 '08 #1
3 1874
On 24 Apr., 07:59, GAURAV AGRAWAL <g...@buffalo.eduwrote:
Hi Guys,
Can someone please explain me why this is happening

#include<iostream>
using namespace std;

class a {

public:
int a1; * * // If I remove this it'll work fine
a() {
* * * * cout << "Constructor of a \n";
* * *}
~a() {
* * * * * cout << "Destructor of a \n";
* * *}

};

class b : public a{

public:
b() {
* cout << "constructor of b \n";}

virtual ~b() { * *// If I remove virtual from here ... it'll work fine
* cout << "destructor of b\n";

}
};

int main() {
* a *p = new b;

* delete p;

* return 0;

}

Result is :
Constructor of a
constructor of b
Destructor of a
a.out(1563) malloc: *** error for object 0x100154: Non-aligned pointer
being freed
*** set a breakpoint in malloc_error_break to debug
----------------------------------------------------------------

I know that if I'll put virtual in front of the destructor of the base
class code will work fine. But, I want to use it the above way. Can
someone please explain me the reason behind this kind of behavior?
Let's say it bluntly: You cannot do it the way you want. You _have_ to
make the destructor of the base class virtual, or you'll get in
trouble when you try to delete derived objects through a base class
pointer. I cannot cite the paragraphs of the C++ standard which say
that you have to do it this way, so I'll try to explain it with my own
words: If you were the compiler and should delete a pointer to 'a',
you'll either (A) call a's destructor right away if 'a' is a class
without virtual destructor, or else (B) look up a's virtual method
table to get the address of the most derived destructor (it your case
b's destructor). If 'a' has no virtual destructor, the compiler
doesn't reserve an entry for destructors in a's VMT, even though
derived classes may have virtual destructors (this is a bit of an
optimization, as it may be the case that 'a' is not derived at all).

Regards,
Stuart
Jun 27 '08 #2
On Apr 24, 7:59 am, GAURAV AGRAWAL <g...@buffalo.eduwrote:
Can someone please explain me why this is happening
#include<iostream>
using namespace std;
class a {
public:
int a1; // If I remove this it'll work fine
a() {
cout << "Constructor of a \n";
}
~a() {
cout << "Destructor of a \n";
}
};
class b : public a{
public:
b() {
cout << "constructor of b \n";
}
virtual ~b() { // If I remove virtual from here ... it'll work fine
cout << "destructor of b\n";
}
};
int main() {
a *p = new b;
delete p;
return 0;
}
Result is :
Constructor of a
constructor of b
Destructor of a
a.out(1563) malloc: *** error for object 0x100154: Non-aligned pointer
being freed
*** set a breakpoint in malloc_error_break to debug
----------------------------------------------------------------
I know that if I'll put virtual in front of the destructor of
the base class code will work fine. But, I want to use it the
above way. Can someone please explain me the reason behind
this kind of behavior?
Simple. It's undefined behavior. So the compiler can assume
that you don't do it, and not take any steps that would
otherwise be necessary for it to work. The standard is very
clear about this: for a non-array delete, either the dynamic
type is the same as the static type, the destructor is virtual,
or you have undefined behavior. (For an array delete, the
dynamic and static types must be the same, period.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
On Apr 24, 4:18 pm, James Kanze <james.ka...@gmail.comwrote:
On Apr 24, 7:59 am, GAURAV AGRAWAL <g...@buffalo.eduwrote:
Can someone please explain me why this is happening
#include<iostream>
using namespace std;
class a {
public:
int a1; // If I remove this it'll work fine
a() {
cout << "Constructor of a \n";
}
~a() {
cout << "Destructor of a \n";
}
};
class b : public a{
public:
b() {
cout << "constructor of b \n";
}
virtual ~b() { // If I remove virtual from here ... it'll work fine
cout << "destructor of b\n";
}
};
int main() {
a *p = new b;
delete p;
return 0;
}
Result is :
Constructor of a
constructor of b
Destructor of a
a.out(1563) malloc: *** error for object 0x100154: Non-aligned pointer
being freed
*** set a breakpoint in malloc_error_break to debug
----------------------------------------------------------------
I know that if I'll put virtual in front of the destructor of
the base class code will work fine. But, I want to use it the
above way. Can someone please explain me the reason behind
this kind of behavior?

Simple. It's undefined behavior. So the compiler can assume
that you don't do it, and not take any steps that would
otherwise be necessary for it to work. The standard is very
clear about this: for a non-array delete, either the dynamic
type is the same as the static type, the destructor is virtual,
or you have undefined behavior. (For an array delete, the
dynamic and static types must be the same, period.)

--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

The same is the behavior for a virtual method which is not the
destructor.
The process crashes when delete statement is encountered.
I suppose there should be some problem in deleting the extra
memory(the virtual pointer) used by the object of type b with a
pointer of type type a.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
3
by: Juha Nieminen | last post by:
This is a simplified version of the situation (the actual situation is quite more complex, but when stripped to the bare minimum, it's like this): class BaseClass { public: virtual void...
8
by: lmfmaw | last post by:
Hi all, I've hit the wall with this "fairly" simple problem. As you can see in the code below, my destructors don't do their job as they are supposed to (I left them empty for this example). I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.