diff --git a/src/refcount.c b/src/refcount.c index bac38f6..db604d6 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -765,11 +765,17 @@ bool refcount_context_add_destructor(const RefcountContext *ctx, void *obj, } entry->callback = callback; entry->user_data = user_data; - if (!ht_insert(ENTRY->destructors, key, entry)) { + if (!lock_entry_mtx(ENTRY)) { refcount_free(&ctx->alloc, entry); return false; } - return true; + bool success = true; + if (!ht_insert(ENTRY->destructors, key, entry)) { + refcount_free(&ctx->alloc, entry); + success = false; + } + unlock_entry_mtx(ENTRY); + return success; } /** @@ -782,5 +788,10 @@ bool refcount_context_add_destructor(const RefcountContext *ctx, void *obj, */ bool refcount_context_remove_destructor(const RefcountContext *ctx, void *obj, void *key) { - return ht_remove(ENTRY->destructors, key); + if (!lock_entry_mtx(ENTRY)) { + return false; + } + bool success = ht_remove(ENTRY->destructors, key); + unlock_entry_mtx(ENTRY); + return success; }