1 2 | Animation animation { heap }; defer { CleanUpAnimation(animation); }; |
You'll notice that I'm explicitly identifying within the animation constructor that memory will be allocated on the heap and I do this to signal to myself that I need to think about releasing/cleaning up this memory. With this cue I can just add a defer { cleanup() } right under it so I get the same functionality as RAII if that's what I want or I could call defer somewhere else (outer scope of the function) if for some reason I don't want to release memory on this particular functions end.
Right now it seems that this way is more explicit and more flexible than something like RAII which to me is a good thing. However, I haven't worked on many serious, large projects yet and I know that a lot of what C++ and other languages laud is making memory/resource management a problem of the particular class with which memory is allocated in, thus ensuring resources are more reliably released and the burden of resource management is not placed on the programmer. Again though, to me making explicit the process of resource acquisition and management is preferred because the programmer should know when memory is being allocated and released and it doesn't seem that difficult a burden for the programmer to have to think about.
What are some of your opinions on the matter? I just hear so many different things from different experts and find it difficult to come to any conclusions.