I know what RAII does. It is all about preventing memory leaks etc. when/if a code throws an exception.
Now, I wish to understand the meaning of that smart term. http://en.wikipedia.org/wiki/Acquisition
Acquisition means acquiring something.
So, when we say that resource acquiring is initialization, what does that mean?
I am just talking about the meaning of the term here, not about the concept in general.
Resource Acquisition focuses on defining the needs for the project, and obtaining the right resources for the team and other resources and tools available to manage the effort.
The principle that objects own resources is also known as "resource acquisition is initialization," or RAII. When a resource-owning stack object goes out of scope, its destructor is automatically invoked. In this way, garbage collection in C++ is closely related to object lifetimeobject lifetimeIn object-oriented programming (OOP), the object lifetime (or life cycle) of an object is the time between an object's creation and its destruction.https://en.wikipedia.org › wiki › Object_lifetimeObject lifetime - Wikipedia, and is deterministic.
RAII is an important C++ idiom for resource management. Notably, RAII provides a structural idiom for proper resource management with exceptions. The power of the idiom is in the guarantees it provides. Properly used, the destructor for your RAII-object is guaranteed to be called to allow you to free resources.
RAII is about automatic release of acquired resources in destructor - there is a run-time guarantee that destructor will be called before object instance is going away regardless of specific mechanism being used. So it should be used always.
It has been said before (possibly by Scott Meyers, I can't remember), that RAII should be called "Destruction is resource release", or words to that effect.
What "resource acquisition is initialization" literally means is that when an object is constructed (initialized), it acquires some resource (such as a memory allocation or a lock). In other words, it says you should only ever acquire a resource, by initializing some object whose destructor will release it.
This is important to stress because it's a departure from C coding style, where you acquire resources by whatever means a particular API provides (for example malloc()
, accept()
, or pthread_mutex_lock()
), and release them by explicitly calling the corresponding function (for example free()
, close()
, pthread_mutex_unlock()
). The presence of exceptions in C++ makes this approach fairly unworkable. Even in C it results in some tedious code that every use of the API has to write out, and every user has to ensure that control always passes through that code after they're finished using the resource.
But the important part of the pattern is that when the object is destroyed, it releases that resource. It doesn't actually matter whether you acquire the resource by initializing the object, or by doing something else with the object after it has been initialized. And people will still refer to an object as a "RAII object" when there are operations other than initialization that generate the resource(s) managed by the RAII object.
So, don't worry too much about the "acquisition is initialization" in "RAII", because anyway it's slightly misleading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With