In C++ you may use
RAII[
^]. Do initialization in the constructor, do some cleanup in the destructor. Take special care of exceptions in the destructor.
In C you must tell the client of your code to follow the protocol: call first some invented
Init(...) function, then at the end some invented
Release(...) function. Both functions are part of your library and do what you require in your question.
Cheers
Andi