raii

Is relying on the type of a Windows handle being a pointer ok?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 22:35:38
Windows handles are sometimes annoying to remember to clean up after (doing GDI with created pens and brushes is a great example). An RAII solution is great, but is it really that great making one full (Rule of Five) RAII class for each different type of handle? Of course not! The best I can see would be one full generic RAII class with other classes just defining what to do when the handle should be cleaned up, as well as other handle-specific aspects. For example, a very simple module class could be defined like this (just an example): struct Module { Module() : handle_{nullptr} {} Module

Why the Destructor in C++ de-allocated memory in reverse order of how they were initialised?

无人久伴 提交于 2019-12-05 12:48:39
问题 What is the advantage in de-allocating memory in reverse order to variables? 回答1: Consider this example: Type1 Object1; Type2 Object2(Object1); Suppose that Object2 uses some internal resources of Object1 and is valid as long as Object1 is valid. For example, Object2 s destructor accesses Object1 's internal resource. If it weren't for the guarantee of reverse order of destruction, this would lead to problems. 回答2: It's not just about deallocating memory, it's about symmetry in a broader

Why can't Alexandrescu use std::uncaught_exception() to implement SCOPE_FAIL in ScopeGuard11? [duplicate]

断了今生、忘了曾经 提交于 2019-12-04 22:52:13
This question already has answers here : Scope(failure) in C++11? (2 answers) Closed 6 years ago . Many people are no doubt familiar with Mr. Alexandrescus ScopeGuard template (now part of Loki) and the new version ScopeGuard11 presented here: http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexandrescu-Systematic-Error-Handling-in-C with source here: https://gist.github.com/KindDragon/4650442 In his talk at c++ and beyond 2012 he mentioned that he couldn't find a way to correctly detect if scope was being exited because of an exception. Therefore he couldn't implement a

Forcing the order in which struct fields are dropped

China☆狼群 提交于 2019-12-04 18:37:46
问题 I'm implementing an object that owns several resources created from C libraries through FFI. In order to clean up what's already been done if the constructor panics, I'm wrapping each resource in its own struct and implementing Drop for them. However, when it comes to dropping the object itself, I cannot guarantee that resources will be dropped in a safe order because Rust doesn't define the order that a struct's fields are dropped. Normally, you would solve this by making it so the object

Smart pointers with a library written in C

喜夏-厌秋 提交于 2019-12-04 14:22:47
问题 I'm using C++ with the OpenCV library, which is a library image-processing although that's not relevant for this question. Currently I have a design decision to make. OpenCV, being a C library, has its data structures (such as CvMat) declared as structs. To create them, you use functions like cvCreateMat, and to release them, you use functions like cvReleaseMat. Being a C++ programmer, I created a special cv_scoped class which would automatically call cvReleaseMat when it went out of scope

When is it appropriate to use C++ exceptions?

↘锁芯ラ 提交于 2019-12-04 13:12:10
I'm trying to design a class that needs to dynamically allocate some memory.. I had planned to allocate the memory it needs during construction, but how do I handle failed memory allocations? Should I throw an exception? I read somewhere that exceptions should only be used for "exceptional" cases, and running out of memory doesn't seem like an exceptional case to me.. Should I allocate memory in a separate initialization routine instead and check for failures and then destroy the class instance gracefully? Or should I use exceptions instead? The class won't have anything useful to do if these

How to encapsulate a C API into RAII C++ classes?

六月ゝ 毕业季﹏ 提交于 2019-12-04 11:35:38
问题 Given a C API to a library controlling sessions that owns items, what is the best design to encapsulate the C API into RAII C++ classes? The C API looks like: HANDLE OpenSession(STRING sessionID); void CloseSession(HANDLE hSession); HANDLE OpenItem(HANDLE hSession, STRING itemID); void CloseItem(HANDLE hItem); Plus other functions that are useful for one of these types (Session, or Item) and map directly to C++ member functions of the relevant object. But they are not needed here. My main

RAII-style C++ class for linked list Nodes

﹥>﹥吖頭↗ 提交于 2019-12-04 10:40:26
I'm playing with linked lists as an exercise at the moment. The examples I'm looking at in the Cracking The Coding Interview book have no LinkedList (manager) class, just Nodes, and you hang on to the head Node in your main function. I looked up C++ implementations, but most seem to be more C-style than C++, i.e. not object-oriented. They use structs, no classes, and have a static method for deleting the list, which you need to explicitly remember to call. I wanted to write a sensible RAII (Resource Acquisition Is Initialization) style C++ class with sensible destructors to handle memory

Locking a mutex in a destructor in C++11

拜拜、爱过 提交于 2019-12-04 08:41:15
问题 I have some code which need to be thread safe and exception safe. The code below is a very simplified version of my problem : #include <mutex> #include <thread> std::mutex mutex; int n=0; class Counter{ public: Counter(){ std::lock_guard<std::mutex>guard(mutex); n++;} ~Counter(){ std::lock_guard<std::mutex>guard(mutex);//How can I protect here the underlying code to mutex.lock() ? n--;} }; void doSomething(){ Counter counter; //Here I could do something meaningful } int

Destructors not called when native (C++) exception propagates to CLR component

≯℡__Kan透↙ 提交于 2019-12-04 08:34:23
问题 We have a large body of native C++ code, compliled into DLLs. Then we have a couple of dlls containing C++/CLI proxy code to wrap the C++ interfaces. On top of that we have C# code calling into the C++/CLI wrappers. Standard stuff, so far. But we have a lot of cases where native C++ exceptions are allowed to propagate to the .Net world and we rely on .Net's ability to wrap these as System.Exception objects and for the most part this works fine. However we have been finding that destructors of