raii

How to make a Rust singleton's destructor run?

て烟熏妆下的殇ゞ 提交于 2019-12-12 18:56:05
问题 These are the ways I know of to create singletons in Rust: #[macro_use] extern crate lazy_static; use std::sync::{Mutex, Once, ONCE_INIT}; #[derive(Debug)] struct A(usize); impl Drop for A { fn drop(&mut self) { // This is never executed automatically. println!( "Dropping {:?} - Important stuff such as release file-handles etc.", *self ); } } // ------------------ METHOD 0 ------------------- static PLAIN_OBJ: A = A(0); // ------------------ METHOD 1 ------------------- lazy_static! { static

Is it necessary to have static constructors when we follow the “RAII ” way of doing things in C++?

南楼画角 提交于 2019-12-12 17:01:55
问题 If I were to follow the RAII rule and would be developing a class in C++, would it be necessary to have static constructors? Will static constructors help me in any way or would be a wrong step to do? 回答1: I take it you are talking about a static factory function that creates an instance of your class (As others pointed out). In which case, you don't need to use the RAII pattern. Remember you need your class to be stack allocated, so that the constructor is called (automatically) and

Poco AsyncChannel does not exit on forked process exit

馋奶兔 提交于 2019-12-12 04:53:45
问题 Problem: Call/Initialize Poco code from inside a C library, using loader (3rd party C program, our code is in c++). The Program loads our library, our lib initializes Poco::Logger and uses AsyncChannel + FileChannel in the logger. Fork from this process. Run some threads in the child process. try to exit the child process, joining threads. The AsyncChannel's destructor blocks on close(), essentially polling Queue and sleeping. The _queue has 1 element always when the destructor for

Scoped mutex lock

怎甘沉沦 提交于 2019-12-11 18:13:05
问题 I never really worked with mutexes before, but i need to control access to protected resources. Looking through the new C++11 stuff, i cooked up this class: class CMutex { public: class Lockable { friend class CMutex; std::atomic_flag flag; public: Lockable() { flag.clear(); } }; private: Lockable * resource; CMutex(const CMutex &); public: CMutex(Lockable * l) { resource = l; acquire(l); } CMutex(Lockable & l) { resource = &l; acquire(l); } CMutex() : resource(nullptr) { } ~CMutex() { if

Making a non-object resource RAII-compliant

孤街浪徒 提交于 2019-12-11 12:31:28
问题 in my code I use HANDLE s from windows.h . They are used like HANDLE h; if (!openHandleToSomething(arg1, arg2, &h)) { throw std::exception("openHandleToSomething error"); } /* Use the handle in other functions which can throw as well */ if (!CloseHandle(h)) { throw std::exception("closeHandle error"); } As you see, you have to insert this CloseHandle to every exception which can happen in the middle of acquiration and release. Therefore, it's likely you forget one (or there is a fancy SEH

Constructor/destructor order dependencies between data members [closed]

泪湿孤枕 提交于 2019-12-11 11:24:56
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I'm creating a C++ class for executing SSH commands, using libssh2. The life cycle of a libssh2 SSH session goes through these stages: Initialization (acquires local resources) Handshake/Authentication (establishes an SSH session on the remote host) Disconnect (terminates SSH

void* pointer returned from Function - Heap Corruption

Deadly 提交于 2019-12-11 07:26:28
问题 Previously, I'd just dealt with these types of __out function parameters using malloc, but I'm trying to change my ways. As a specific example, in a class to manage Raw Input, GetRawInputDeviceInfo() is prototyped as such: UINT GetRawInputDeviceInfo(HANDLE, UINT, LPVOID, PUINT) LPVOID is a pointer to a buffer containing the information I need. PUINT is a pointer to a UINT containing the size of data contained in the buffer pointed to by LPVOID. Normally, I would (once I have populated the

Thrown object cannot be caught in a multi-threaded solution

安稳与你 提交于 2019-12-11 03:03:11
问题 I have a RAII class that solves a problem in an inner thread: #include <iostream> #include <thread> using namespace std; struct solution_using_thread { solution_using_thread() : alive_(true), thread_() { thread_ = thread([this]() { while(alive_); }); } ~solution_using_thread() { alive_ = false; thread_.join(); } private: bool alive_; thread thread_; }; int main() { cout << 0 << endl; try { solution_using_thread solution; throw 1; } catch (int i ) { cout << i << endl; } cout << 2 << endl; }

RAII for resources that can be invalidated

痞子三分冷 提交于 2019-12-11 02:28:01
问题 I'm a hobbyist C++ and DirectX programmer, so most of the knowledge I have is from old game development books in which the code designs are just to get something up and running as a demonstration, leaving me with a lot of design considerations for even the simplest of programs. During development of such a program, I recently learned of RAII , so I decided to give this design pattern a shot because from what I understood, an object should be usable and valid upon construction and this greatly

Freeing allocated memory

这一生的挚爱 提交于 2019-12-11 02:02:57
问题 Is this good practice? Or should I just replace the code block between { and } with a function? It could be reusable (I admit) but my only motivation for doing this is to deallocate colsum since it's huge and not required so that I can free the allocated memory. vector<double> C; { vector<double> colsum; A.col_sum(colsum); C = At*colsum; } doSomething(C); 回答1: Using brackets to scope automatic variables is fine in my book, but generally if you find yourself doing it a lot, especially multiple