M
Michael Klatt
I have a resource handle class (call it ResourceHandle) that works
like std::auto_ptr to automatically manage resource pointers from a C
API (call them T*):
class ResourceHandle
{
public :
ResourceHandle(T* ptr) { /* take ownership of T* */ }
~ResourceHandle() { /* release T* resource */ }
};
ResourceHandle f() { /* return a new T* */ }
Function f() relies on ResourceHandle(T*) being called as a result of
the return statement to avoid a resource leak. But what happens if
the return value of f() is not used in the calling function?
int main()
{
ResourceHandle resource(f()); // no leak
f(); // resource leak if return statement is elided?
}
like std::auto_ptr to automatically manage resource pointers from a C
API (call them T*):
class ResourceHandle
{
public :
ResourceHandle(T* ptr) { /* take ownership of T* */ }
~ResourceHandle() { /* release T* resource */ }
};
ResourceHandle f() { /* return a new T* */ }
Function f() relies on ResourceHandle(T*) being called as a result of
the return statement to avoid a resource leak. But what happens if
the return value of f() is not used in the calling function?
int main()
{
ResourceHandle resource(f()); // no leak
f(); // resource leak if return statement is elided?
}