If you shouldn’t throw exceptions in a destructor, how do you handle errors in it?

Throwing an exception out of a destructor is dangerous. If another exception is already propagating the application will terminate. #include <iostream> class Bad { public: // Added the noexcept(false) so the code keeps its original meaning. // Post C++11 destructors are by default `noexcept(true)` and // this will (by default) call terminate if an exception … Read more

RAII and smart pointers in C++

A simple (and perhaps overused) example of RAII is a File class. Without RAII, the code might look something like this: File file(“/path/to/file”); // Do stuff with file file.close(); In other words, we must make sure that we close the file once we’ve finished with it. This has two drawbacks – firstly, wherever we use … Read more

What is the best workaround for the WCF client `using` block issue?

Given a choice between the solution advocated by IServiceOriented.com and the solution advocated by David Barret’s blog, I prefer the simplicity offered by overriding the client’s Dispose() method. This allows me to continue to use the using() statement as one would expect with a disposable object. However, as @Brian pointed out, this solution contains a … Read more

What is a dangling pointer?

A dangling pointer is a pointer that points to invalid data or to data which is not valid anymore, for example: Class *object = new Class(); Class *object2 = object; delete object; object = nullptr; // now object2 points to something which is not valid anymore This can occur even in stack allocated objects: Object … Read more