Keep a reference count on the native objects

When creating a snapshot or an iterator, increase the reference count of
the database or snapshot it's based on. Close the native resource when
it's refcount reaches 0, rather than explicitely closing the wrapper.