Hacker News new | ask | show | jobs
by presidentscroob 2485 days ago
It's a good pattern to DRY-up resource-releasing unwind code. In an ideal world, there would be closures, RAII and fluent interfaces (macros) that would wrap allocation, deallocation, error and success handling without needing C++ and avoiding resource leaks.

    int
    some_func(void) {
      int result = ERR;

      HANDLE foo = alloc_foo();
      if (!foo) goto err0;

      HANDLE bar = alloc_bar();
      if (!bar) goto err1;

      if (do_something_else() < 0) goto err2;

      result = OK;
    err2:
      release_bar(bar);
    err1
      release_foo(foo);
    err0:
      return result;
    }