In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "C++ why all resources requested by the class must be released in the destructor", in daily operation, I believe many people in C++ why all resources requested by the class must be released in the destructor There are doubts on the issue, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "C++ why all resources requested by the class must be released in the destructor" doubts helpful! Next, please follow the small series to learn together!
All resources requested by the class must be released in the destructor
Reason
Avoid resource leakage, especially in case of errors.
Note:
This happens automatically if the resource appears as a class that implements the full set of default actions.
Example
class X { ifstream f; // may own a file // ... no default operations defined or =deleted ...};
An ifstream member of class X implicitly closes any file it opens through a destructor.
Example, bad
class X2 { // bad FILE* f; // may own a file // ... no default operations defined or =deleted ...};
X2 may leak a file handle.
X2 has the potential to leak file handles. (This is usually the case with missing close files.)
Note:
What if sokcet wasn't shut down? First, destructors, close or clear operations should never fail. If it does fail, there is no really good solution to the problem. For the initiator module, the author of the destructor does not know what the destructor is called for, and there is no way to "reject processing" by throwing an exception. Even worse, many Close/Release operations cannot be retried. There have been many attempts to solve this problem, but no universal solution has been found. If possible, failure to close or purge can be treated as a fundamental error and terminated.
Note:
A class can hold pointers or references to objects it does not own. Obviously, such objects should not be destroyed by destructors of this class. For example:
Preprocessor pp { /* ... */ };Parser p { pp, /* ... */ };Type_checker tc { p, /* ... */ };
Here p points to pp but does not own pp.
Enforcement
(Simple) If a class has pointer or reference member variables that are owners (e.g., deemed owners by using gsl::owner), then they should be referenced in its destructor.
(Simple) If a class contains pointer or reference members that have ownership (e.g., declared by gsl::owner), they should be referenced in the destructor.
Translator's note: I think it should be released in the destructor.
(Hard) Determine if pointer or reference member variables are owners when there is no explicit statement of ownership (e.g., look into the constructors).
(Difficulty) Determining whether pointer or reference type member variables are owners when they do not explicitly state ownership (e.g., by walking through constructors, etc.).
At this point, the study of "C++ why all resources requested by classes must be released in destructors" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.