Eng: Modern C++/RAII

RAII stands for Resource Acquisition Is Initialization, which you only need to remember for trivia night at your local C++ watering hole: it’s so far outgrown the acronym that the acronym itself has become misleading.

The original idea was that a constructor allowed you to initialize resources in the same moment they were acquired, getting rid of the old C-style “first you declare your variable, then you forget to initialize it, then you spend the rest of the day tracking down a hard-to-recreate bug brought on by whatever random pattern of bits was in memory there”. See? Resource acquisition is initialization. Your constructor initializes things on the spot, you never have to deal with this kind of bug.

But then we started looking at the flip side of it, and realized destructors were every bit as important as constructors. If we could guarantee object initialization through constructors, what about guaranteeing object cleanup through destructors?

Consider this bit of Python code:

Garbage collection

What happens under garbage collection? The short answer is, we don’t know. Maybe, if we’re lucky, Python will run its garbage collector frequently enough that we’ll never exhaust our open file handles. Maybe if we’re unlucky Python will run its garbage collector infrequently enough that we exhaust our handles in a heartbeat. Although a good garbage collector guarantees our resources will be collected, few garbage collectors make any guarantees about when they will be collected: and that’s a problem for inherently limited resources like file handles, network sockets, database connections, and more.

On the other hand, consider what a proper C++ destructor can do:

RAII

Here, every time our while loop finishes the foo destructor gets called — which releases the megabyte of memory we acquired. This is essentially a very fast spin that never holds onto more than a megabyte of memory, with behavior guaranteed by the C++ standard.

RAII should really be called “predictable collection”, but it’s not. Sorry about that. But now you know what the hype is about and why modern C++ puts such an emphasis on, “if it’s a limited resource, wrap it in an object that has a proper destructor”.


2 Comments

  1. You started out making me think, “this time RH got it wrong.” But I get your point. I don’t fully agree with it though. RAII is a name that has well understood meaning among C++ programmers who pay attention. For those who don’t, it is really easy to explain the benefits as you have without choosing a different name. As the symbol we use to communicate a big concept, RAII works quite well.

    Something that does bother me is how often I interview job candidates who don’t know the term or the concept. They do get it quickly when the concept is explained and many of them have been using it without having a name for it, but the lack of familiarity with common nomenclature often surprises me. Another term that stumps many C++ programmers is Abstract Data Type – but that’s a different topic.

    • My objection to RAII is entirely in the learning curve for newcomers. Deterministic destruction is a fine thing, but the acronym doesn’t appear connected to it and it usually requires some explaining. I’m all about making names map closely to concepts. 🙂

Leave a Reply