This still won't `close()` if `data` is bad and this prematurely exits. You want, once it's been determined that the file is valid:

    #include "folly/ScopeGuard.h"

    ...

    SCOPE_EXIT {
      f->close();
    };