In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "C++ destructor function, memory release and swap operation analysis". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
E.16: Destructors, memory deallocation and swap operations must never fail
Reason
We don't know how to write a trustworthy handler if a destructor, swap operation, or memory release fails; that is, if it exits with an exception or simply doesn't perform the required operation.
Example, don't
class Connection {
// ...
public:
~Connection() // Don't: very bad destructor
{
if (cannot_disconnect()) throw I_give_up{information};
// ...
}
};
Note:
Many have tried to write reliable code violating this rule for examples, such as a network connection that "refuses to close". To the best of our knowledge nobody has found a general way of doing this. Occasionally, for very specific examples, you can get away with setting some state for future cleanup. For example, we might put a socket that does not want to close on a "bad socket" list, to be examined by a regular sweep of the system state. Every example we have seen of this is error-prone, specialized, and often buggy.
There have been many attempts to write examples of trustworthy code that violate this rule, such as the network link "refuse to close." Even with all the knowledge, no one has found a universal way to do so. Occasionally, for very special cases, you can do this by setting certain states to be used in future cleanup processing. For example, we can put unwanted sockets into a list of "bad sockets" that can be checked with a regular system state cleanup program. All of the examples we see are error-prone, idiosyncratic, and often buggy.
Note:
The standard library assumes that destructors, deallocation functions (e.g., operator delete), and swap do not throw. If they do, basic standard-library invariants are broken.
The standard library assumes that destructors, memory-freeing functions (such as the delete operator), and swap do not throw exceptions. If they are abnormal, the invariants of the standard library are destroyed.
Note:
Deallocation functions, including operator delete, must be noexcept. swap functions must be noexcept. Most destructors are implicitly noexcept by default. Also, make move operations noexcept.
Memory deallocation functions that contain the delete operator must not throw exceptions. The swap function must not throw an exception. Most destructors default
Enforcement
Catch destructors, deallocation operations, and swaps that throw. Catch such operations that are not noexcept.
Catches destructors that throw exceptions, memory free operations, and swap functions. Catches cases where these operations are not declared noexcept.
"C++ destructor, memory release and swap operation analysis" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality 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.