Fix refcounting in object

This commit also fixes unref().
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.
Merge pull request #497 from neithernut/fix-refcount_locking

Fix refcounting in object