Re:Show HN: Boot Windows 10 Directly into Linux Desktop of C...
by int_19h on Thursday March 02, 2017 @12:44AM
(#13771752)
The more typical coding style for raw Win32 is to have a block of CloseHandle/Free/... block at the end of the function, and goto it for early exit. Usually with some helper macros to easily write "if it failed, goto cleanup" one-liners.
[ Parent
| on HN ]
Re:Show HN: Boot Windows 10 Directly into Linux Desktop of C...
by richardwhiuk on Thursday March 02, 2017 @04:05AM
(#13772709)
You can't have a single cleanup (because subsequent calls use memory/handles from prior - otherwise you'd have a function wrapper), so it ends up getting really nasty - at which point, the logic feels clearer (and easier to match alloc and free) if you have indented blocks. The change I'd make to the code would be to have a shorter indent block.
HRESULT hr;
hr = DoFoo();
if (!SUCCESS(hr))
{
goto exit;
}
hr = DoBar();
if (!(SUCCESS(hr))
{
goto cleanup_foo;
}
hr = DoBar();
if (!(SUCCESS(hr))
{
goto cleanup_bar;
}
hr = DoBaz();
if (SUCCESS(hr))
{
CleanupBaz();
}
cleanup_bar:
CleaupBar();
cleanup_foo:
CleanupFoo();
exit:
return hr;
Re:Show HN: Boot Windows 10 Directly into Linux Desktop of C...
by int_19h on Thursday March 02, 2017 @08:19PM
(#13779993)
> You can't have a single cleanup (because subsequent calls use memory/handles from prior
Why not? You just initialize them all to NULL (or INVALID_HANDLE_VALUE etc, as appropriate). And then in your cleanup block, you check whether each was initialized, and clean it up. So long as you consistently do this in reverse order of their initialization, it works.