raii

How do I run a cleanup code on the function exit?

戏子无情 提交于 2019-12-20 04:22:54
问题 C++ classes provide RAII idiom. Therefore you don't have to care about exceptions: void function() { // The memory will be freed automatically on function exit std::vector<int> vector(1000); // Do some work } But if you have (for some reasons) to use some pure C API, you have either to create C++ wrappers around it or to use try/catch blocks void function() { int *arr = (int*)malloc(1000*sizeof(int)); if (!arr) { throw "cannot malloc"; } try { // Do some work } catch (...) { free(arr); //

Is a C++ destructor guaranteed not to be called until the end of the block?

穿精又带淫゛_ 提交于 2019-12-18 18:37:15
问题 In the C++ code below, am I guaranteed that the ~obj() destructor will be called after the // More code executes? Or is the compiler allowed to destruct the obj object earlier if it detects that it's not used? { SomeObject obj; ... // More code } I'd like to use this technique to save me having to remember to reset a flag at the end of the block, but I need the flag to remain set for the whole block. 回答1: You are OK with this - it's a very commonly used pattern in C++ programming. From the C+

Why is RAII so named? [closed]

混江龙づ霸主 提交于 2019-12-18 15:52:56
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . The sense I get about this idiom is that it is useful because it ensures that resources are released after the object that uses them goes out of scope. In other words, it's more about de-acquisition and de-initialisation , so why is this idiom named the way it is? 回答1: First, I should note that it's widely

How should one log when an exception is triggered?

南楼画角 提交于 2019-12-18 07:46:16
问题 In a program I recently wrote, I wanted to log when my "business logic" code triggered an exception in third-party or project APIs. ( To clarify, I want to log when use of an an API causes an exception. This can be many frames above the actual throw , and may be many frames below the actual catch ( where logging of the exception payload can occur. ) ) I did the following: void former_function() { /* some code here */ try { /* some specific code that I know may throw, and want to log about */

Typedef a shared_ptr type with a static custom deleter, similar to unique_ptr

独自空忆成欢 提交于 2019-12-18 06:57:48
问题 I have read through many questions on SO on custom deleter for shared_ptr and unique_ptr , and the difference between the two. But, I still haven't found any clear answer to this question: How can one best go about creating a type that acts as a shared_ptr with a custom deleter, similar to how unique_ptr has the deleter as part of the type definition? For unique_ptr usage, I use a deleter class, that handles deletion of individual types (limiting it to just two types, for brevity): struct SDL

Pattern name for create in constructor, delete in destructor (C++)

前提是你 提交于 2019-12-18 04:04:21
问题 Traditionally, in C++, you would create any dependencies in the constructor and delete them in the destructor. class A { public: A() { m_b = new B(); } ~A() { delete m_b; } private: B* m_b; }; This technique/pattern of resource acquisition, does it have a common name? I'm quite sure I've read it somewhere but can't find it now. Edit: As many has pointed out, this class is incomplete and should really implement a copy constructor and assignment operator. Originally, I intentionally left it out

Can Tail Call Optimization and RAII Co-Exist?

末鹿安然 提交于 2019-12-18 01:43:11
问题 I can't think of a true RAII language that also has tail call optimization in the specs, but I know many C++ implementations can do it as an implementation-specific optimization. This poses a question for those implementations that do: given that destructors are invoked at the end of a automatic variable's scope and not by a separate garbage collection routine, doesn't it violate TCO's constraint that a recursive call must be the last instruction at the end of a function? For example:-

RAII wrapper for OpenGL objects

南楼画角 提交于 2019-12-17 15:38:32
问题 I want to write a simple RAII wrapper for OpenGL objects (textures, frame buffers, etc.) I have noticed, that all glGen* and glDelete* functions share the same signature, so my first attempt was like this: typedef void (__stdcall *GLGenFunction)(GLsizei, GLuint *); typedef void (__stdcall *GLDelFunction)(GLsizei, const GLuint *); template <GLGenFunction glGenFunction, GLDelFunction glDelFunction> class GLObject { GLuint m_name; public: GLObject() { glGenFunction(1, &m_name); } ~GLObject() {

c++ new C array allocation, RAII or simple shared_ptr / boost::shared_array

℡╲_俬逩灬. 提交于 2019-12-14 00:35:52
问题 I am learning c++ and i stumbled once again on a new issue. I do need to allocate a C array for a library to use, but in a safe way, ofcourse. I already found that delete[]; at the end of method fails miserably. OLD, not that good: float *buf; try { buf = new float[daswidth*chans_info.chans*sizeof(float)]; } catch (std::bad_alloc& ba) // sometimes it throws ! so i do need to stop my execution. { if (DEBUG) tekstasFormat(L"WARNING: bad alloc caught: %s", ba.what()); return; // skip this

Idiomatic short lifespan local objects akin to RAII

假如想象 提交于 2019-12-12 21:28:18
问题 I came across this fragment of Objective-C: NSNumber *theBalance = [[[NSNumberFormatter alloc] init] numberFromString: [textField text]]; This seems to leak the NSNumberFormatter . In C++ I would do one of two things: use auto (i.e. stack) storage for the NSNumberFormatter use RAII (e.g. shared_ptr ) to manage the life of the NSNumberFormatter In Objective-C neither of these options seem to be possible. I tried on the stack: NSNumberFormatter fmt; But this doesn't compile. As far as I can