And another note on the dereferencing. In the Qt code are some nice tools that allow you to do things like atomicly increment+fetch. They've implemented those for all platforms they care for (which is actually a lot). i.e. [here](http://qt.gitorious.org/qt/qtbase/blobs/3f1a4be30263a676b7e188bf6cfa809c7788c87f/src/corelib/arch/qatomic_x86_64.h#line298)

If one is able to _atomically_ increment the ref-counter (by -1) and at the same time test it for a given value (there's methods for such operations in qt, just look around a bit on QAtomicInt and it's various implementations), you can be sure to only free the associated memory in one thread, even in race conditions that are allowed by the code as it is in libgit2.