Hacker News new | past | comments | ask | show | jobs | submit login

I maintain a combined error and resource state per thread.

It is first argument to all functions.

If no errors, function proceeds - if error, function instead simply immediately returns.

When allocating resource, resource is recorded in a btree in the state.

When in a function an error occurs, error is recorded in state; after this point no code executes, because all code runs only if no errors.

At end of function is boilerplate error, which is added to error state if an error has occurred. So for example if we try to open file and out of disk, we first get error "fopen failed no disk", then second error "opening file failed", and then all parent functions in the current call stack will submit their errors, and you get a call stack.

Program then proceeds to exit(), and immediately before exit frees all resources (and in correct order) as recorded in btree, and prints error stack.




Why bother freeing before exit?


Assuming that this would include properly closing sockets, etc. which the OS doesn't do the same way you might want to




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: