Fix several leaks from ensure_gridfs_index() and cleanup the cursor