Deep Analysis of C ++ destructor-Good Practice: declare the destructor of the base class as a virtual function

Source: Internet
Author: User
All C ++ programmers are familiar with destructor, so they can be applied quickly due to their simplicity and ease of understanding. I will not talk about these common methods here. If you do not know, refer to C ++ books. What I want to talk about this time is a more subtle technique that is often not noticed, but is very important. See the following code:
//\\////\\////\\////\\////\\////\\//\\// //\\////\\//
// Example 1
// Author: Yuan Kai
// Date: 2001-09-24
//\\////\\////\\////\\////\\////\\//\\// //\\////\\//

# Include <iostream. h>

Class cfunction
{
Public:
Cfunction ()
{
Data = new char [64];
};
~ Cfunction ()
{
Delete [] data;
};
Char * data;
};

Class cfunctionex: Public cfunction
{
Public:
Cfunctionex ()
{
M_data = new char [64];
};
~ Cfunctionex ()
{
Delete [] m_data;
};
PRIVATE:
Char * m_data;
};

Void main ()
{
Cfunction * pcfun = new cfunctionex;
Delete pcfun;
}
Can you see the problem? Obviously, there is a memory leak. This is because when pcfun is deleted, it only calls the cfunction destructor and does not call the cfunctionex destructor, resulting in Memory leakage. Let's look at the following example:
//\\////\\////\\////\\////\\////\\//\\// //\\////\\//
// Example 2
// Author: Yuan Kai
// Date: 2001-09-24
//\\////\\////\\////\\////\\////\\//\\// //\\////\\//
# Include <iostream. h>
Class cbase
{
Public:
Cbase ()
{
Data = new char [64];
};
~ Cbase ()
{
Delete [] data;
};
Char * data;
};
Class cfunction
{
Public:
Cfunction (){};
~ Cfunction (){};
};
Class cfunctionex: Public cfunction
{
Public:
Cfunctionex (){};
~ Cfunctionex (){};
PRIVATE:
Cbase m_cbase;
};
Void main ()
{
Cfunction * pcfun = new cfunctionex;
Delete pcfun;
}
Can you see the problem? The cfunctionex and cfunction do not allocate memory here, so there should be no memory leakage. As in the previous example, when pcfun is deleted, it only calls the cfunction destructor and does not call the cfunctionex destructor. However, cfunctionex does not allocate memory, where is there a memory leak? I don't mean everyone should know. The good news is m_cbase, because it is a cbase instance and a cfunctionex member variable. When the number of destructor of cfunctionex is not called, of course, m_cbase's destructor is not called either, therefore, the memory allocated in the cbase is leaked.
The solution to the above problem is very simple, that is, to make the analysis function of the base class cfunction as a virtual function. Simple, right? Haha ......
In this way, we can draw a conclusion that when the destructor of your base class is not virtual,
1.1 The Memory allocated in its subclass may leak.
2.2 The Memory allocated in the class of all member variables in its subclass may also leak.
The second point is very important because it is easy to be missed. This is why I wrote this article.
This may be because, if the program does not write similar statements in the above example (the base class pointer is used to point to the sub-class instance margin, the virtual function is the essence of C ++, few do not use it, because it is in large and medium-sized software projects), there will be no memory leakage mentioned in this article. It seems that it is so important to make the Destructor a virtual function in the base class. Therefore, we strongly recommend that you declare the Destructor as a virtual function in the base class, except when only the class you write is not implemented as the base class.
The above problems I encountered at work, the program tested in VC ++ 6, memory leakage is very important for an efficient service program. I think you may have encountered this problem, so I hope to help you write this article. Please forgive me for any mistakes or mistakes in writing the article. Welcome to your discussion.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.