473,769 Members | 3,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual destructor problem

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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..

Please can someone help me clean up properly?
--------

#include <iostream>
using namespace std;

class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;

};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;

};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);

for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
Dec 9 '07 #1
8 1839
On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:
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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..

Please can someone help me clean up properly?
--------

#include <iostream>
using namespace std;

class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;

};

class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;

};

int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);

for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;

}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.
Dec 9 '07 #2
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:


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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}

As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:

delete anBase[0];
delete anBase[1];

Dec 9 '07 #3
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:
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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -

There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:

delete anBase[0];
delete anBase[1];
In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...
Dec 9 '07 #4
On Dec 9, 5:52 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:


On Dec 9, 5:34 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:
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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:
delete anBase[0];
delete anBase[1];

In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...

The negative impact is that it is undefined behaviour as per the
standards. What bad will happen depends on the particular
implementation. May cause a memory leak. I cannot comprehend what else
can go wrong in cases of undefined behaviours.
Dec 9 '07 #5
On Dec 9, 6:02 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 9, 5:52 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:
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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:
delete anBase[0];
delete anBase[1];
In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...

The negative impact is that it is undefined behaviour as per the
standards. What bad will happen depends on the particular
implementation. May cause a memory leak. I cannot comprehend what else
can go wrong in cases of undefined behaviours.
Oh, if that is the case, why does't the standard mandate dest to be
always virtual when some other function is also virtual in the base
class? It would be easy for the compiler to spot out the error too,
isn't it?
Dec 9 '07 #6
Rahul wrote:
:: On Dec 9, 6:02 pm, Abhishek Padmanabh
:: <abhishek.padma n...@gmail.comw rote:
::: On Dec 9, 5:52 pm, Rahul <sam_...@yahoo. co.inwrote:
:::
:::
:::
:::: On Dec 9, 5:47 pm, Abhishek Padmanabh
:::: <abhishek.padma n...@gmail.comw rote:
:::
::::: On Dec 9, 5:34 pm, Rahul <sam_...@yahoo. co.inwrote:
:::
:::::: On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:
:::
::::::: 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 running the Visual Leak Detector library (on VS 2005) to
::::::: detect memory leaks and whatever I've tried it still
::::::: complains..
:::
::::::: Please can someone help me clean up properly?
::::::: --------
:::
::::::: #include <iostream>
::::::: using namespace std;
:::
::::::: class Base {
::::::: public:
::::::: Base(int baseInt) { m_baseInt=baseI nt; }
::::::: virtual ~Base() {}
::::::: virtual void behave() const { cout << "I'm Base." <<
::::::: endl; } virtual void prnt() const { cout << m_baseInt <<
::::::: endl; } int m_baseInt;
:::
::::::: };
:::
::::::: class Derived : public Base {
::::::: public:
::::::: Derived(int baseInt, int derivedInt): Base(baseInt)
::::::: { m_derivedInt=de rivedInt; }
::::::: ~Derived() {}
::::::: void behave() const { cout << "I'm Derived!" << endl; }
::::::: void prnt() const { cout << m_baseInt << " : " <<
::::::: m_derivedInt << endl; }
::::::: private:
::::::: int m_derivedInt;
:::
::::::: };
:::
::::::: int main() {
::::::: Base *anBase[2];
::::::: anBase[0] = new Base(11);
::::::: anBase[1] = new Derived(100, 1907);
:::
::::::: for(int i = 0; i < 2; i++) {
::::::: anBase[i]->behave();
::::::: anBase[i]->prnt();
::::::: }
::::::: return 0;
:::
::::::: }
:::
:::::: As your derived class as well as base class destructor's
:::::: aren't doing
:::::: anything, there is no need for them to be declared as
:::::: virtual.- Hide quoted text -
:::
::::: There is "need" for the base destructor to be declared virtual
::::: because otherwise the following will not work:
:::
::::: delete anBase[0];
::::: delete anBase[1];
:::
:::: In the sense, it just wouldn't invoke the derived class
:::: destructor and as the derived class destructor is not doing
:::: anything.
:::: I don't see any negative impact. Yes i'm not saying it is a thumb
:::: rule...
:::
::: The negative impact is that it is undefined behaviour as per the
::: standards. What bad will happen depends on the particular
::: implementation. May cause a memory leak. I cannot comprehend what
::: else can go wrong in cases of undefined behaviours.
::
:: Oh, if that is the case, why does't the standard mandate dest to be
:: always virtual when some other function is also virtual in the base
:: class? It would be easy for the compiler to spot out the error too,
:: isn't it?

But it is only an error if you do a delete through a pointer to base.
If you just have a local Derived variable, or use a pointer to
Derived, it is ok.
Bo Persson
Dec 9 '07 #7
On Dec 9, 7:52 am, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:47 pm, Abhishek Padmanabh <abhishek.padma n...@gmail.com>
wrote:
On Dec 9, 5:34 pm, Rahul <sam_...@yahoo. co.inwrote:
On Dec 9, 5:16 pm, lmf...@yahoo.co m wrote:
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 running the Visual Leak
Detector library (on VS 2005) to detect memory leaks and whatever I've
tried it still complains..
Please can someone help me clean up properly?
--------
#include <iostream>
using namespace std;
class Base {
public:
Base(int baseInt) { m_baseInt=baseI nt; }
virtual ~Base() {}
virtual void behave() const { cout << "I'm Base." << endl; }
virtual void prnt() const { cout << m_baseInt << endl; }
int m_baseInt;
};
class Derived : public Base {
public:
Derived(int baseInt, int derivedInt): Base(baseInt)
{ m_derivedInt=de rivedInt; }
~Derived() {}
void behave() const { cout << "I'm Derived!" << endl; }
void prnt() const { cout << m_baseInt << " : " << m_derivedInt <<
endl; }
private:
int m_derivedInt;
};
int main() {
Base *anBase[2];
anBase[0] = new Base(11);
anBase[1] = new Derived(100, 1907);
for(int i = 0; i < 2; i++) {
anBase[i]->behave();
anBase[i]->prnt();
}
return 0;
}
As your derived class as well as base class destructor's aren't doing
anything, there is no need for them to be declared as virtual.- Hide quoted text -
There is "need" for the base destructor to be declared virtual because
otherwise the following will not work:
delete anBase[0];
delete anBase[1];

In the sense, it just wouldn't invoke the derived class destructor and
as the derived class destructor is not doing anything.
I don't see any negative impact. Yes i'm not saying it is a thumb
rule...
If you allocate and store objects using a pointer to a base class,
the standard requires the base class destructor to be virtual.

Don't get fooled by an empty d~tor body, they may now (or later in
your developmental process) invoke other destructors (ie: member
d~tors). In this scenario you have integer members so those d~tors are
definitely doing something.
Dec 9 '07 #8
On 2007-12-09 16:48:00 -0500, Salt_Peter <pj*****@yahoo. comsaid:
>
If you allocate and store objects using a pointer to a base class,
the standard requires the base class destructor to be virtual.
No, it doesn't. The relevant requirement is that if you delete an
object of a derived type through a pointer to one of its base types and
that base type does not have a virtual destructor, the behavior of the
program is undefined. That is, a virtual destructor is required if you
delete the object through a pointer to base. Not if you only allocate
and store it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 10 '07 #9

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

Similar topics

23
2157
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 the case when the class contains at least one virtual member function? Shouldn't the destructor always be virtual, whenever there's a possibility that an inherited object will be destructed through a base class pointer? (This does not require,
11
4368
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 it inline. Like this.
37
4178
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
3984
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 add a virtual destructor like this : " virtual ~vechile". I get this error: Undefined first referenced symbol in file vtable for vechile /var/tmp//ccC9yD6Z.o
3
2068
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 destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
5
11360
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 to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
17
3547
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;" instead? I think declaring a function as "=0" is the same
3
1388
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 cleanup(); virtual ~BaseClass() { cleanup(); } };
3
1909
by: GAURAV AGRAWAL | last post by:
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
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.