diff options
author | David Robillard <d@drobilla.net> | 2011-03-07 18:54:42 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-03-07 18:54:42 +0000 |
commit | 5d1d788b442bf53e3910d69d8cad214df63b47f8 (patch) | |
tree | 0fa98e6e88b50baa4afaa036ea13488a7ec4b3ce /src | |
parent | 3e4fc1e4b1f4e65bd4c9f6c9e9a4c753c10232a6 (diff) | |
download | sord-5d1d788b442bf53e3910d69d8cad214df63b47f8.tar.gz sord-5d1d788b442bf53e3910d69d8cad214df63b47f8.tar.bz2 sord-5d1d788b442bf53e3910d69d8cad214df63b47f8.zip |
Fix reference counting and memory leaks.
git-svn-id: http://svn.drobilla.net/sord/trunk@50 3d64ff67-21c5-427c-a301-fe4f08042e5a
Diffstat (limited to 'src')
-rw-r--r-- | src/sord.c | 6 | ||||
-rw-r--r-- | src/sord_test.c | 5 |
2 files changed, 8 insertions, 3 deletions
@@ -771,7 +771,7 @@ sord_new_node(SordNodeType type, const uint8_t* data, size_t n_bytes) SordNode node = malloc(sizeof(struct _SordNode)); node->type = type; node->n_bytes = n_bytes; - node->refs = 0; + node->refs = 1; node->datatype = 0; node->lang = 0; node->buf = (uint8_t*)g_strdup((const char*)data); // TODO: no-copy @@ -845,7 +845,6 @@ sord_node_get_datatype(SordNode ref) static void sord_add_node(SordWorld world, SordNode node) { - node->refs = 0; ++world->n_nodes; } @@ -921,7 +920,8 @@ sord_node_free(SordWorld world, SordNode node) return; } - if (node->refs == 0 || (--node->refs == 0)) { + assert(node->refs > 0); + if (--node->refs == 0) { if (node->type == SORD_LITERAL) { if (!g_hash_table_remove(world->literals, node)) { fprintf(stderr, "Failed to remove literal from hash.\n"); diff --git a/src/sord_test.c b/src/sord_test.c index 840b1e7..9d607fc 100644 --- a/src/sord_test.c +++ b/src/sord_test.c @@ -67,7 +67,11 @@ generate(SordWorld world, SordModel sord, size_t n_quads, size_t n_objects_per) for (size_t j = 0; j < n_objects_per; ++j) { SordQuad tup = { ids[0], ids[1], ids[2 + j] }; sord_add(sord, tup); + sord_node_free(world, ids[2 + j]); } + + sord_node_free(world, ids[0]); + sord_node_free(world, ids[1]); } // Add some literals @@ -77,6 +81,7 @@ generate(SordWorld world, SordModel sord, size_t n_quads, size_t n_objects_per) tup[2] = sord_new_literal(world, 0, (const uint8_t*)"hello", NULL); tup[3] = 0; sord_add(sord, tup); + sord_node_free(world, tup[2]); tup[2] = sord_new_literal(world, 0, USTR("hi"), NULL); sord_add(sord, tup); |