Fix several leaks from ensure_gridfs_index() and cleanup the cursor
Merged pull request #269