Why garbage collection when RAII is available?

Garbage collection and RAII are useful in different contexts. The presence of GC should not affect your use of RAII. Since RAII is well-known, I give two examples where GC is handy. Garbage collection would be a great help in implementing lock-free data structures. […] it turns out that deterministic memory freeing is quite a … Read more

Does Java support RAII/deterministic destruction?

EDIT: The answer below was written in early 2009, when Java 7 was very much still in flux. While Java still doesn’t provide guarantees around finalization timing, it did gain a feature like C#’s using statement: the try-with-resources statement. No, Java hasn’t changed at all in that respect. You still need to use try/finally. There’s … Read more

RAII vs. exceptions

You SHOULD NOT throw an exception out of a destructor. Note: Updated to refeclt changes in the standard: In C++03 If an exception is already propagating then the application will terminate. In C++11 If the destructor is noexcept (the default) then the application will terminate. The Following is based on C++11 If an exception escapes … Read more

C++ read the whole file in buffer [duplicate]

There’s no need for wrapper classes for very basic functionality: std::ifstream file(“myfile”, std::ios::binary | std::ios::ate); std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); std::vector<char> buffer(size); if (file.read(buffer.data(), size)) { /* worked! */ }

Are destructors called after a throw in C++?

Yes, it is guaranteed (provided the exception is caught), down to the order in which the destructors are invoked: C++11 15.2 Constructors and destructors [except.ctor] 1 As control passes from a throw-expression to a handler, destructors are invoked for all automatic objects constructed since the try block was entered. The automatic objects are destroyed in … Read more

Implementing RAII in pure C?

This is inherent implementation dependent, since the Standard doesn’t include such a possibility. For GCC, the cleanup attribute runs a function when a variable goes out of scope: #include <stdio.h> void scoped(int * pvariable) { printf(“variable (%d) goes out of scope\n”, *pvariable); } int main(void) { printf(“before scope\n”); { int watched __attribute__((cleanup (scoped))); watched = … Read more

Is it abusive to use IDisposable and “using” as a means for getting “scoped behavior” for exception safety?

I consider this to be an abuse of the using statement. I am aware that I’m in the minority on this position. I consider this to be an abuse for three reasons. First, because I expect that “using” is used to use a resource and dispose of it when you’re done with it. Changing program … Read more

Understanding the meaning of the term and the concept – RAII (Resource Acquisition is Initialization)

So why isn’t that called “using the stack to trigger cleanup” (UTSTTC:)? RAII is telling you what to do: Acquire your resource in a constructor! I would add: one resource, one constructor. UTSTTC is just one application of that, RAII is much more. Resource Management sucks. Here, resource is anything that needs cleanup after use. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)