Fix handle sweep code: make sure we're closing the right data handle, check the reference count before freeing, and use atomic increment to match the (unlocked) atomic decrement on close.

refs #844