2 ms·
"RAII" describes what you have to do to make resource releasing work correctly in C++ when there are exceptions all over the place. If you first acquire a reso
by kragen 5y ago
"RAII" describes what you have to do to make resource releasing work correctly in C++ when there are exceptions all over the place. If you first acquire a resource and then later initialize an object with it, or assign it to an object, there's a time window when the resource has been acquired but is not yet owned by the object, during which an exception will cause it to leak. If, for example, the object's operator= raises an exception before safely storing the resource, you lose.
Alternative names like "constructor acquires, destructor releases" might be better, but I think "RAII" was sort of devised as a slogan to shout at people so they would fix their code.
- pjmlp 5y agoRAII was invented before C++ considered exceptions. Back in MS-DOS/Windows 3.x, and Mac OS, CFront/UNIX days, RAII was used for file handles, network connections, other sort of OS handles, and naturally data structures like dynamic strings and arrays. When exceptions came into scene, RAII was already established as pattern.
- kragen 5y agoI think you're using the term "RAII" in a much looser sense, and we didn't call it that at the time (though I don't have a convenient way to demonstrate this now that Google Groups has nerfed their search). C++ code from that period (and Google C++ code to this day) violates RAII regularly by acquiring resources in non-initialization contexts. RAII without exceptions is pretty inconvenient: acquiring resources can almost always fail (the only exception I can think of is locks) and so you need some kind of return value, which is precisely what you don't have in a constructor. So you need a flag value in the initialized object which you later have to check, which means that all your resourcy objects are "nullable", in the sense that the type system can't guarantee you that they're in a meaningful state, and you need a dynamic check. Maybe you mean that people commonly used destructors for cleanup before C++ had exceptions, and I certainly agree with that. But using destructors for cleanup is not the same thing as RAII. Without RAII, without exceptions, without destructor cleanup, perfectly OK: int fd = open(fname, O_RDONLY); if (fd < 0) return FAIL; ... close(fd); return OK; Without RAII, without exceptions, with destructor cleanup, perfectly OK: int fd = open(fname, O_RDONLY); if (fd < 0) return FAIL; File file = fd; ... return OK; Without RAII, with exceptions, terrible buggy dangerous code: int fd = open(fname, O_RDONLY); if (fd < 0) return FAIL; File file = fd; ... return OK; Note that this is exactly the same as the previous "perfectly OK" example! You can try to fix this up with catch(...) { close(fd); throw; } but the correctness of such convolutions depends intimately on the precise contours of how exceptions can happen, if at all, in the File constructor. With RAII, without exceptions, terrible buggy dangerous code: File file(fname, O_RDONLY); ... return OK; Correct version: File file(fname, O_RDONLY); if (!file.ok) return FAIL; ... return OK; With RAII, with exceptions, perfectly OK: File file(fname, O_RDONLY); ... return OK; Note that this is exactly the same as the "terrible buggy dangerous code" above. The difference is only that now the File constructor signals failure by throwing an exception instead of setting a flag. This is also the most efficient of all the versions on the happy path because it doesn't waste memory on success flags or conditional jumps (in this function and in its caller) on checking them. It's also the most concise and, arguably, the least error-prone. (Google policy disagrees.)