I'm well aware of the RAII pattern and std::unique_ptr
and other "smart pointers" in C++11, but there's still some cases which I can't figure out how to handle nicely without having a goto Cleanup
section which does the cleanup finally.
Specifically, I'm thinking about Windows programming where sometimes i'll want to leak handles and other times I won't.
If I have a function that looks kind of like he following:
PROCESS_INFORMATION p;
if (!CreateProcess(... &p))
{
throw windows_error(GetLastError());
}
DWORD r = WaitForSingleObject(h, 5000);
if (r == WAIT_TIMEOUT || r == WAIT_FAILED)
{
// terminate process, close handles
throw ...;
}
if (!SetEnvironmentVariable(...))
{
// terminate process, close handles
throw windows_error;
}
(a few other operations that if they fail i have cleanup to do).
return process handle;
I don't really see how unique_ptr
can help me here, unless i use release()
on the unique_ptr
after all the if
to indicate success/tell the unique_ptr
to not clean it up. (I can create a special deleter for unique_ptr
so it cleans up windows handles properly). However, my question is that is such a usage of release()
on smart pointers "correct" for cases where i want to leak allocated memory/handles back to the caller? Is there a better way to do it? Returning a shared_ptr
can work too, I suppose...