From 7f1d856ea4882a23461886d0f589244cc11f0b2f Mon Sep 17 00:00:00 2001 From: Petr Machata Date: Thu, 13 Dec 2012 18:22:32 +0100 Subject: Fix memory errors With this, valgrind run of ltrace on x86_64 is clean. --- prototype.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'prototype.c') diff --git a/prototype.c b/prototype.c index bb13525..1ce6ae7 100644 --- a/prototype.c +++ b/prototype.c @@ -142,6 +142,8 @@ protolib_init(struct protolib *plib) dict_hash_string, dict_eq_string, NULL); VECT_INIT(&plib->imports, struct protolib *); + + plib->refs = 0; } static void @@ -159,6 +161,8 @@ destroy_named_type_cb(struct named_type *named, void *data) void protolib_destroy(struct protolib *plib) { + assert(plib->refs == 0); + VECT_DESTROY(&plib->imports, struct prototype *, NULL, NULL); DICT_DESTROY(&plib->prototypes, const char *, struct prototype, @@ -323,7 +327,11 @@ destroy_protolib_cb(struct protolib **plibp, void *data) { assert(plibp != NULL); assert(*plibp != NULL); - protolib_destroy(*plibp); + + if (--(*plibp)->refs == 0) { + protolib_destroy(*plibp); + free(*plibp); + } } void @@ -621,6 +629,8 @@ protolib_cache_protolib(struct protolib_cache *cache, int rc = DICT_INSERT(&cache->protolibs, &filename, &plib); if (rc < 0 && own_filename) free((char *)filename); + if (rc == 0) + plib->refs++; return rc; } -- cgit v1.2.3