Fix refcounting in object

Previously, we had a lock for refcounting but didn't use it _at all_.
This commit introduces proper recount-locking and thus fixes a _lot_ of
potential issues.

Targets #495.