Fix improper loop condition caused segfault in hdb_destroy
Fix hdb_destroy error handling in the wrong place