From 46c54f834c37df0324aea37ba099f01b043668a8 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sat, 11 Jan 2014 16:19:17 +1000 Subject: jim.c: Fix Jim_ReplaceHashEntry() for ref counted objects If both a val dup and a val destructor exist, need to dup before destroying. Signed-off-by: Steve Bennett --- jim.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'jim.c') diff --git a/jim.c b/jim.c index cd6e616..72a8e95 100644 --- a/jim.c +++ b/jim.c @@ -820,16 +820,28 @@ int Jim_ReplaceHashEntry(Jim_HashTable *ht, const void *key, void *val) * the element already exists. */ entry = JimInsertHashEntry(ht, key, 1); if (entry->key) { - /* It already exists, so replace the value */ - Jim_FreeEntryVal(ht, entry); + /* It already exists, so only replace the value. + * Note if both a destructor and a duplicate function exist, + * need to dup before destroy. perhaps they are the same + * reference counted object + */ + if (ht->type->valDestructor && ht->type->valDup) { + void *newval = ht->type->valDup(ht->privdata, val); + ht->type->valDestructor(ht->privdata, entry->u.val); + entry->u.val = newval; + } + else { + Jim_FreeEntryVal(ht, entry); + Jim_SetHashVal(ht, entry, val); + } existed = 1; } else { /* Doesn't exist, so set the key */ Jim_SetHashKey(ht, entry, key); + Jim_SetHashVal(ht, entry, val); existed = 0; } - Jim_SetHashVal(ht, entry, val); return existed; } -- cgit v1.2.3