From d018f0d4fc8c35a3070834d8d3a2f7dd1b9d2f39 Mon Sep 17 00:00:00 2001 From: Guo Rui Date: Tue, 28 Jul 2009 01:14:43 +0800 Subject: [PATCH] Unplug broker from the hash on object invalidation. This solve the problem comming along with reusing the same memory for an irrelevant object. The new object will correctly have a separate broker now. --- src/script.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/script.c b/src/script.c index 10203ad..7b91e6d 100644 --- a/src/script.c +++ b/src/script.c @@ -362,6 +362,12 @@ get_obj_broker(void *obj) tmp->valid = 1; tmp->b_next = NULL; } + else + { + /* Assertion */ + if (!(*bucket)->valid) + exit(1); + } (*bucket)->ref++; return *bucket; @@ -383,9 +389,14 @@ void * get_broker_obj(struct broker **pbroker) void broker_inv_obj(void *obj) { - struct broker **b = broker_from_obj(obj); - if (*b) + struct broker *n, **b = broker_from_obj(obj); + if (*b) { (*b)->valid = 0; + /* Unplug the broker from the hash. */ + n = *b; + *b = (*b)->b_next; + n->b_next = 0; + } } void broker_unref(struct broker **pbroker) @@ -394,14 +405,19 @@ void broker_unref(struct broker **pbroker) *pbroker = NULL; if (--broker->ref == 0) { - struct broker *n, **b = broker_from_obj(broker->obj); - if (*b) { - n = (*b)->b_next; - free(*b); - *b = n; + if (broker->valid) { + struct broker *n, **b = broker_from_obj(broker->obj); + if (*b) { + n = (*b)->b_next; + free(*b); + *b = n; + } else { + /* FIXME:Should not happen! But act better.*/ + exit(1); + } } else { - /* FIXME:Should not happen! But act better.*/ - exit(1); + /* It had been unplugged before. */ + free(broker); } } } -- 2.11.4.GIT