- Joined
- Oct 8, 2005
- Messages
- 121
- Reaction score
- 23
Changed due incorrections , something i was read and know now its not so right.
Last edited:
Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!
Join Today!You really did not capture the essence of RAII, which is also described the linked reference. Plus, your explanation is almost impossible to understand. Maybe it's a language issue.
Anyaway, RAII is described in the bullet points.
The thing about resource is, that you must always release them once you're done with them. Many languages have constructs for that, e.g. a with block in Python or a try-finally block in Java. In C++ you often use RAII, which can be used in all object oriented languages. This ties the lifetime of the resources to an object. The lifetime of the objects are in-turn managed by the language, most often the stack or some sort of smart pointer. In the above example it is the stack.
In C++ when you create an object in a block (e.g. a function) without explicitly putting it on the heap (e.g. with new), it will be on the stack and its destructor will be called once the instruction pointer leaves the block. This ensures that the resources are freed. This is the reason you do not call any explicit unlock function in the good function of the above example. The compiler inserts those.
The first example is bad because if the if-statement evaluates to true, you return and you'd never hit that free, therefore your muted would be locked. This could be very difficult to find in a larger code base.