build: fix build with debug enabled
ref_count: Add a rough debugging support.

This debugging support is very rough and inaccurate, and add a huge
overhead.
directory, db: fix a dir_handle reference leak.
directory: fix several reference leaks and crashes.