Hacker News new | ask | show | jobs
by int_19h 3400 days ago
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.
1 comments

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;
> 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.