#138 - only call defer f.Close() when needed
Merge pull request #141 from couchbaselabs/feature/issue_138_invalid_memory

#138 - only call defer f.Close() when needed