raii

RAII tutorial for c++

Hi. I'd like to learn how to use RAII in c++. I think I know what it is, but have no idea how to implement it in my programs. A quick google search did not show any nice tutorials. Does any one have any nice links to teach me RAII? ...

Better way to write an object generator for an RAII template class?

I would like to write an object generator for a templated RAII class -- basically a function template to construct an object using type deduction of parameters so the types don't have to be specified explicitly. The problem I foresee is that the helper function that takes care of type deduction for me is going to return the object by ...

How to handle failure to release a resource which is contained in a smart pointer?

How should an error during resource deallocation be handled, when the object representing the resource is contained in a shared pointer? EDIT 1: To put this question in more concrete terms: Many C-style interfaces have a function to allocate a resource, and one to release it. Examples are open(2) and close(2) for file descriptor...

~1s latency control app: is this suitable for Java?

At my work we recently finished the system architecture for a control application which has a maximum latency of roughly one to two seconds. It is distributed on small ARM on-chip boxes communicating via an IP LAN. We initially foresee that we would use C or C++, since it is a classical control system language. After discussing how to ...

Throwing Exception in CTOR and Smart Pointers

Is it OK to have the following code in my constructor to load an XML document into a member variable - throwing to caller if there are any problems: MSXML2::IXMLDOMDocumentPtr m_docPtr; //member Configuration() { try { HRESULT hr = m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument40)); ...

Howto mix TDD and RAII

I'm trying to make extensive tests for my new project but I have a problem. Basically I want to test MyClass. MyClass makes use of several other class which I don't need/want to do their job for the purpose of the test. So I created mocks (I use gtest and gmock for testing) But MyClass instantiate everything it needs in it's constructo...

Getting shared_ptr to call a member function once its reference count reaches 0

I'm creating a wrapper for a HANDLE that does not work with DuplicateHandle, so instead I am trying to wrap the handle in a shared_ptr. Imagine the following code: class CWrapper { public: CWrapper() : m_pHandle(new HANDLE, &CWrapper::Close) { //code to open handle } private: void Close() { ...

C++ Raii and stack unwinding

hello (I modified the original question to be more meaningful) With respect to return statement, are Raii object destroyed before/after/between return statement? for example size_t advance() { boost::lock_guard<boost::mutex> lock(mutex_); return value_++; // is lock destroyed after increment? } thank you ...

Freeing allocated memory

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 = A...

Can you use a shared_ptr for RAII of C-style arrays?

I'm working on a section of code that has many possible failure points which cause it to exit the function early. The libraries I'm interacting with require that C-style arrays be passed to the functions. So, instead of calling delete on the arrays at every exit point, I'm doing this: void SomeFunction(int arrayLength) { shared_ptr...

RAII and assignment

I created the following class for an sqlite3 connection: class SqliteConnection { public: sqlite3* native; SqliteConnection (std::string path){ sqlite3_open_v2 (path.c_str(), &native, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); } ~SqliteConnection (){ sqlite3_close(native); } } and then one...

When RAII is not needed

Hi, Have you had any case when RAII wasn't the best method for resource management? Just couriosity... Thanks. ...

Way for C++ destructor to skip work when specific exception being thrown?

I have an object on the stack for which I wish its destructor to skip some work when the destructor is being called because the stack is being unwound due to a specific exception being thrown through the scope of the object on the stack. Now I could add a try catch block inside the scope of the stack item and catch the exception in ques...

Exception within function returning value for constructor...

Let's say I have class that acts as a "smart pointer" and releases some kind of system resource when destroyed. class Resource{ protected: ResourceHandle h; public: Resource(ResourceHandle handle) :h(handle){ } ~Resource(){ if (h) releaseResourceHandle(h);//external function, probably from...

Using RAII with C++ streams and STL containers?

I'm trying to use RAII concepts with an STL container of ofstream objects. For example: int main(int argc, char**argv) { std::deque<std::ofstream> sList; sList.push_back(std::ofstream()); // tried variations such as *(new ofstream()) sList[0].open("test1.txt"); sList[0] << "This is a test"; sList[0].close(); } However, no ...

Is there any research on (or better use of) of RAII in GC languages?

Note: Object Lifetime RAII not using/with block scope RAII It seems like its possible using an extra gc category, short lived objects(check gc category somewhat frequently), long lived objects(check gc category less frequently), and resource objects(check gc category very frequently). Or possibly with an extra reference counting gc for ...

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

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 th...

How to react to stack unwinding when destructors are not supported by a language?

Suppose you have created an instance of a Window class. The window is shown to the user. Then, an exception is thrown, and reference to the instance is lost, but the window is still seen by the user because the instance still exists (it's just not referenced anymore). What to do in these circumstances? I'm specifically talking about th...

Stack-based object instantiation in D

I'm learning D, and am confused by an error I'm getting. Consider the following: module helloworld; import std.stdio; import std.perf; ptrdiff_t main( string[] args ) { auto t = new PerformanceCounter; //From managed heap //PerformanceCounter t; //On the stack t.start(); writeln( "Hello, ", size_t....

When a RAII object fails to construct

Suppose I construct a RAII object, and that object may fail to construct. How do I handle this? try { std::vector<int> v(LOTS); // try scope ends here because that's what the catch is for } catch( const std::bad_alloc& ) { // ... } // v? what v? Granted, the default constructor of std::vector won't throw and that can help,...