473,396 Members | 1,815 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.

how to use the virtual destructor?

10
Hi all,
I have a piece of code like this
constructor:

IPCClientDataTransfer::IPCClientDataTransfer (RWCString serviceName) :Valid(FALSE)
{
NSTcpProfile tcpProfile;
int clientPriority = 1;
tcpProfile.BlockingOnConnect = TRUE;
tcpProfile.Blocking = TRUE;
strcpy(tcpProfile.ServiceName, serviceName);
strcpy(tcpProfile.TcpHostName, "localhost");

this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);

// Check to see if the IPCAMAClient is a valid object
if (!this -> PClient -> isValid())
{
// The object is invalid, the connection was not established
delete this -> PClient;
this -> PClient = NULL;
}
else
{
// The Object is valid
this -> Valid = TRUE;
}
destructor:

IPCClientDataTransfer::~IPCClientDataTransfer ()
{
if (this -> PClient)
delete this -> PClient;
}

the destructor is not virtual.does this means that there will be memory leak?
Apr 3 '08 #1
11 1712
weaknessforcats
9,208 Expert Mod 8TB
There will be no leak in the IPCClientDataTransfer class.

However, if IPCClientDataTransfer has virtual functions and another class derives from IPCClientDataTransfer, you may have a leak.

In this case the derived class object is identified by either a base class reference or a base class pointer (simple C++ polymorphism). Without the virtual destructor in IPCClientDataTransfer, the destructor of the derived class will not be called when the pointer or reference to IPCClientDataTransfer is deleted.
Apr 3 '08 #2
sugarva
10
ok.thanks a lot.
there are no virtual functions in IPCClientDataTransfer.C class.
in this line
this -> PClient = new IPCAMAClient (&tcpProfile, clientPriority);

a new instance has been created for IPCAMAClient class.
but i dont think it is a derived class of IPCClientDataTransfer.C class.

whether still the memory leak problem exist because the IPCClientDataTransfer.C class destructor is not virtual?
Apr 4 '08 #3
sugarva
10
and one more info

IPCClientDataTransfer is wrapper class around IPCAMAClient.
Apr 4 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
From what I see the new IPCAMAClient object allocated in the IPCClientDataTransfer constructor will be deleted by the IPCAMAClient destructor. So you shouldn't leak.

I did notice that in one place you:
// The object is invalid, the connection was not established
delete this -> PClient;
this -> PClient = NULL;
}
but in ther destructor you:
if (this -> PClient)
delete this -> PClient;
It is not required to test for zero before deleting but it is important to zero after deleting in case you delete again. Deleting twice will crash the program.

You do not zero in the destructor. Somebody could call the destructor more than once and your prgram would crash.

In both places I would change to:
Expand|Select|Wrap|Line Numbers
  1. delete this->PClient;
  2. this->PClient = 0;
  3.  
Apr 4 '08 #5
whodgson
542 512MB
I was under the impresion that a virtual destructor is illegal.Is this wrong?
Apr 5 '08 #6
Laharl
849 Expert 512MB
Yes. Virtual constructors are illegal, but virtual destructors are not only legal but also very useful to deal with the inherent base class object created with derived class objects.
Apr 5 '08 #7
whodgson
542 512MB
Thanks......i need to re-read my books and do some more exercises on connstructors and destructors.
Apr 6 '08 #8
sugarva
10
thaks a lot.

you have said that IPCAMAClient destructor will delete the object.

but the destructor is
/************************************************** **************
Method: ~IPCAMAClient

Description: The destructor. Does nothing

Parameters: None

Returns: void
************************************************** **************/
IPCAMAClient::~IPCAMAClient( )
{
}

/************************************************** **************


it does nothing.
is it necessary that i should delete the object here and to zero that?

whether deleting the objects in two classes is necessary?
Apr 8 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
it does nothing.
is it necessary that i should delete the object here and to zero that?

whether deleting the objects in two classes is necessary?
Remember, in C++ constructors do not create objects and destructors do not delete objects.

Only the compiler can create and delete objects. As part of that process, when the object is created the compiler, as a courtesy, will call your constructor so you can initialize the already created object. Similarly, when the object is being deleted the compiler, again as a courtesy, will call your destructor so you can release any memory allocations you may have made.

Constructors and destructors are just functions like any other.

If you happen to call your destructor yourself, then any variables you allocated in the object will be released but your object is still there. Calling a destructor is a convenient way to release the current contents of an object when you intend to install new contents, like with an assignment operator overload.

If your class contains no variables allocated using the new operator, then you don't need a destructor at all.
Apr 8 '08 #10
sugarva
10
ok.thanks.
so,in your opinion there won't be any memory leak.right?
Apr 9 '08 #11
weaknessforcats
9,208 Expert Mod 8TB
Again, if you do not use the new operator, there can never be a leak.

If you so use the new operator and your delete is in your destructor, then there is no leak.

If you use the new operator more than once without deleting the previous allocationm, then you will have a leak.
Apr 9 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
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...
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 ]
4
by: Tony Johansson | last post by:
Hello Experts!! Assume I have a base class called animal. I want this class to be abstract so I make the destructor pure virtual by having this statement. virtual ~Animal() = 0; Destructor...
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...
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...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
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;"...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.