Resource leak

Hello, I just create a helper library to prevent resource leak and posted at https://github.com/TarcioV/ResourceGuard check out and tell me what you think.
Last edited on
Initial impressions from this post is that it's either unique_ptr or a scope guard.

The sample in the description contains this code
1
2
3
4
5
6
7
if(init_device() != OK) {
	throw std::runtime_error("init_device() failed\n");
}

algorithms.add(new ResourceGuard([](){
	close_device();
}));


new can throw exceptions. As can ResourceGuardChain::add. If an exception is raised the device will never be closed.
Last edited on
To avoid resource leak when porting legacy code most time we have to create several micro classes using RAII idom. To prevent the proliferation of these micro classes we could optionally use lambda expressions.
Sure, but now instead of a class, every time you use init_device() you have to remember to queue up a lambda that calls close_device(), and you have to get it right every time use that function, while a class you can write correctly once and forget about it.
Registered users can post here. Sign in or register to post.