How does a C ++ object die? Why write this series?

Source: Internet
Author: User

to explain how C ++ objects die, you must first start with the c ++ destructor. This is a language feature that I like very much. (unfortunately, there are several languages that do not have similar things, so I will not name them.
it will not cause a verbal war ). We can use the constructor and destructor of C ++ to implement the guard mode, write the Code that is clear, concise, and extremely secure. Because guard mode is widely used in C ++ Programs , therefore, ensuring that all objects are destructed
is a very important and serious issue.

In addition, I found that many c ++ programmers only care about memory leakage and do not care (or are unclear) resource leakage (similar to the phenomenon I mentioned in "Java beginners [3]
). For example, how did the "C ++ object" die yesterday? Process
"
after the release, some people asked: the process is dead, and the object is not destroyed. What is the relationship between the pinch? It actually has a lot to do! Although the operating system will send it to the dead after the process is dead (that is, some resources, such as memory, are returned
), there is basically no need to worry about memory leakage. But don't forget that in addition to memory resources, the process may also contain resources at other business layers, and these resources will not be automatically recycled by the operating system. Therefore,
I will try again: resource leakage is often much more serious than memory leakage
. The following example shows how to enhance your impression.
for example, a business logic Foo needs to operate a large number of temporary files (stored in a dynamically generated temporary directory). To ensure that the business logic ends normally, or an exception may be thrown in the middle). The temporary directory is always deleted and the following guard mode can be used.

Class ctempdirguard
{
Public:
Ctempdirguard (const string & sfoldername)
{
// Create a temporary directory
}
Virtual ~ Ctempdirguard ()
{
// Delete the entire temporary directory
}
};

Void Foo ()
{
Ctempdirguard guard (XXX); // declare the guard object
// Put things in the temporary directory
// Guard will be destructed whether a return statement or an exception is thrown, so the xxx directory will be deleted

// However, if the program is executed here, the processNonNatural death,
// In this case, the guard object willNoIs parsed, so a junk directory is left, which wastes hard disk resources.
}

In view of the two reasons mentioned above, I have always wanted to write a post in this regard. I wrote a post just a few days ago to discuss the "multi-process Architecture Design" issue.
", Then I wrote a post by the way:" How is the c ++ process dead ?", The impact of different dead processes on the structure of C ++ objects is discussed. After writing it, I suddenly thought: Except for the termination of the process, the problem may cause the C ++ objectNo
Normal analysis, threads, and other factors may make C ++ objectsNo
Normal structure. So I simply changed the name to "How did the C ++ object die ?"

In addition, to facilitate reading, sort the directories of this series of posts as follows:
1. Process

2. Further exploration of the standard input/output stream

3. Win32 thread

4. POSIX thread (pthread)

5 ,......


Copyright Notice

All original posts in this blogArticleThe author retains the copyright. This statement must be reprinted to keep this article complete, and the author's programming will be noted in the form of hyperlinks
And the original address of this article:

Http://program-think.blogspot.com/2009/02/cxx-object-destroy-overview.html

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.