Or you could use C++ and RAII. It's an excellent fit for the problem that's been presented.
Though I do understand, there are some very special projects that can't use C++, and must use C. I'd say in most cases, the extra machinery you get with C++ outweighs the small performance penalty you get by going with straight C.