How to handle resource cleanup with RAII #4437
Unanswered
edmundsj
asked this question in
Community Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a resource (a server) I am using for my tests with google test. This server can be created as a simple object with RAII. Constructor initializes it and opens a port, destructor closes that port. Clean. Nice. Works well. But when I try to instantiate this inside a google test fixture in the Setup() method, and an exception happens to be thrown in the test itself (which is common - this is the whole point I am writing tests - to identify issues like uncaught exceptions), then the server's destructor is never called and the resource is not cleaned up, and I'm left with a dangling open port and running server after the test.
How am I supposed to handle this use case? The only way I have figured out how to do it is bypassing fixtures altogether, and simply catching and re-throwing the exception within each test case. Seems idiotic. Is there no way to get Google test fixtures to cleanup resources they allocate with RAII when an exception is thrown? Is this a limitation of the language itself, not the framework?
Beta Was this translation helpful? Give feedback.
All reactions