raii

What is the best way to implement smart pointers in C++?

I've been evaluating various smart pointer implementations (wow, there are a LOT out there) and it seems to me that most of them can be categorized into two broad classifications: 1) This category uses inheritance on the objects referenced so that they have reference counts and usually up() and down() (or their equivalents) implemented....

When is it appropriate to use C++ exceptions?

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

side effects of garbage collection?

This may be an eminently closeable question, but I'm the type that sees what sticks to the wall. For all of the benefits of memory and lifetime management afforded by a garbage collected runtime, have there been any notable cases of program indeterminacy caused by race conditions between an application and its garbage collector? Has a ...

Is C++ like resource management possible in Java

In C++ we have Resource Acquisition Is Initialization (RAII) pattern, which extremely simplify the resource management. The idea is to provide some wrapping object for any kind of resources. The wrapping object's destructor is then responsible for releasing the resource, when it's going out of its scope. For example: { auto_ptr<int>...

Please help us non-C++ developers understand what RAII is

Another question I thought for sure would have been asked before, but I don't see it in the "Related Questions" list. Could you C++ developers please give us a good description of what RAII is, why it is important, and whether or not it might have any relevance to other languages? I do know a little bit. I believe it stands for "Resour...

RAII and uninitalized values

Just a simple question: if I had a simple vector class: class Vector { public: float x; float y; float z; }; Doesnt the RAII concept apply here as well? i.e. to provide a constructor to initialize all values to some values (to prevent uninitialized value being used). EDIT or to provide a constructor that explicitly asks the u...

C++ RAII not working??

I'm just getting started with RAII in C++ and set up a little test case. Either my code is deeply confused, or RAII is not working! (I guess it is the former). If I run: #include <exception> #include <iostream> class A { public: A(int i) { i_ = i; std::cout << "A " << i_ << " constructed" << std::endl; } ~A() { std::cout << "A ...

What could be a reason to not use bracket classes in C++?

It's often needed to accomplish the following task: change the state of something, do action, then change the state back to original. For example, in Win32 GDI it's needed to change background color, then do some drawing, then change the color back. It can be either done directly: COLORREF oldColor = SetBkColor( deviceContext, newColor...

Is it possible to kill a C++ application on Windows XP without unwinding the call stack?

My understanding is that when you kill a C++ application through Task Manager in Windows XP, the application is still "cleanly" destructed - i.e. the call stack will unwind and all the relevant object destructors will be invoked. Not sure if my understanding is wrong here. Is it possible to kill such an application immediately, without...

Local variable scope question

Why is the following code prints "xxY"? Shouldn't local variables live in the scope of whole function? Can I use such behavior or this will be changed in future C++ standard? I thought that according to C++ Standard 3.3.2 "A name declared in a block is local to that block. Its potential scope begins at its point of declaration and ends ...

Initialising an anonymous mutex-lock-holding class instance in the LHS of a comma operator

Suppose I have code something like this: #include "boost/thread/mutex.hpp" using boost::mutex; typedef mutex::scoped_lock lock; mutex mut1, mut2; void Func() { // ... } void test_raiicomma_1() { lock mut1_lock(mut1); Func(); } void test_raiicomma_2() { (lock(mut1)), Func(); } void test_raiicomma_3() { (lock(mut1)), (lock(mut2...

Is it possible to prevent an RAII-style class from being instantiated "anonymously"?

Suppose I have an RAII-style C++ class: class StateSaver { public: StateSaver(int i) { saveState(); } ~StateSaver() { restoreState(); } }; ...to be used like so in my code: void Manipulate() { StateSaver save(1); // ...do stuff that modifies state } ...the goal being to enter some state, do stuff, then leave that sta...

C# - Are objects immediately destroyed when going out of scope?

Can I trust that an object is destroyed and its destructor is called immediately when it goes out of scope in C#? I figure it should since many common coding practices (e.g. transaction objects) rely on this behaviour, but I'm not very used to working with garbage collection and have little insight to how such languages usually behave. ...

Can inversion of control and RAII play together?

I was just reading up on inversion of control (IOC) and it bothered me that it seems like it makes memory management a pain. Of course it seems ioc is mostly used in garbage collected environments (Net,Java,Scripting), while my concern is in non-gc settings. My concern here is that IOC in a way goes against RAII, as we decouple resource...

Making a non-object resource RAII-compliant

Hello, in my code I use HANDLEs 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 h...

Making a HANDLE RAII-compliant using shared_ptr with a custom deleter

I've recently posted a general question about RAII at SO. However, I still have some implementation issues with my HANDLE example. A HANDLE is typedeffed to void * in windows.h. Therefore, the correct shared_ptr definition needs to be std::tr1::shared_ptr<void> myHandle (INVALID_HANDLE_VALUE, CloseHandle); Example 1 CreateToolhelp32...

How to fix heap corruption

I've tried to build a very minimalistic memory read library to read some unsigned ints out of it. However, I run into a "HEAP CORRUPTION DETECTED" error message when the ReadUnsignedInt method wants to return. HEAP CORRUPTION DETECTED. CRT detected that the application wrote to memory after end of buffer. As I have read, this may b...

Adding functionality to a handle wrapper.

I have a C++ RAII class for managing Win32 HANDLEs using boost::shared_ptr<> that looks a bit like this: namespace detail { struct NoDelete { void operator()( void* ) {}; }; }; // namespace detail template< typename HANDLE_TYPE, typename HANDLE_DELETER > class CHandleT { public : explicit CHandleT( HANDLE_TYPE handle, bool delete_o...

The Price of DuplicateHandle

Hello :) I'm writing a class library that provides convenient object-oriented frontends to the C API that is the Windows Registry. I'm curious, however, what the best course of action is for handling HREGs, for instances where my key class is copied. I can either Allocate a heap integer and use it as a reference count. Call RegCloseK...

Is RAII safe to use in C#? And other garbage collecting languages?

I was making an RAII class that takes in a System.Windows.Form control, and sets its cursor. And in the destructor it sets the cursor back to what it was. But is this a bad idea? Can I safely rely that the destructor will be called when objects of this class go out of scope? ...