This should be done in a finally block to avoid leaks.