kcat / alure

Alure is a utility library for OpenAL, providing a C++ API and managing common tasks that include file loading, caching, and streaming
zlib License
70 stars 20 forks source link

lack of RAII in examples despite functionality being present? #10

Closed Cazadorro closed 5 years ago

Cazadorro commented 6 years ago

Many of the examples include statements like ctx.destroy() or dev.close() while new is no where to be found, and destruction is being called at the end of scope. Is there a particular reason for this pattern? It appears as if all the objects actually have destructors any way.

kcat commented 6 years ago

Everything is destroyed when the DeviceManager goes out of scope, but the individual things don't before then (except for SharedPtrs and SharedFutures and such, since they're standard smart pointers). For instance, with that example if source.destroy() wasn't called and it looped to play multiple files, it would leak multiple sources prior to the end since there's no way to get back the previously allocated ones.

The more general reason for the lack of RAII is because it's possible for destruction to fail (or even crash) if done incorrectly since devices own contexts, and contexts own sources/buffers/etc. If they're destructed in the wrong order, it will crash since they're effectively pointers to those resources that have been deleted (and I'm not fond of the idea of resources keeping their owners alive after the app has gotten rid of the latter). There is an AutoObj wrapper container that adds RAII behavior for most resources, although the app has to promise it will scope them correctly when using it or else.

Cazadorro commented 5 years ago

Thanks for the detailed reply!