memory leak in hash values fixed
This commit is contained in:
@@ -163,6 +163,8 @@ int hash_add(hash_t *table, const char * const key, void *data)
|
|||||||
entry->next = table->entries[table_index];
|
entry->next = table->entries[table_index];
|
||||||
table->entries[table_index] = entry;
|
table->entries[table_index] = entry;
|
||||||
table->num_keys++;
|
table->num_keys++;
|
||||||
|
} else {
|
||||||
|
if (table->free) table->free(ctx, entry->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
entry->value = data;
|
entry->value = data;
|
||||||
|
|||||||
@@ -16,18 +16,19 @@
|
|||||||
#include "strophe.h"
|
#include "strophe.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
|
#include "test.h"
|
||||||
|
|
||||||
#define TABLESIZE 100
|
#define TABLESIZE 100
|
||||||
#define TESTSIZE 500
|
#define TESTSIZE 500
|
||||||
|
|
||||||
/* static test data */
|
/* static test data */
|
||||||
const int nkeys = 5;
|
|
||||||
const char *keys[] = {
|
const char *keys[] = {
|
||||||
"foo", "bar", "baz", "quux", "xyzzy"
|
"foo", "bar", "baz", "quux", "xyzzy"
|
||||||
};
|
};
|
||||||
const char *values[] = {
|
const char *values[] = {
|
||||||
"wuzzle", "mug", "canonical", "rosebud", "lottery"
|
"wuzzle", "mug", "canonical", "rosebud", "lottery"
|
||||||
};
|
};
|
||||||
|
const int nkeys = ARRAY_SIZE(keys);
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -58,7 +59,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* allocate a hash table */
|
/* allocate a hash table */
|
||||||
table = hash_new(ctx, TABLESIZE, NULL);
|
table = hash_new(ctx, TABLESIZE, xmpp_free);
|
||||||
if (table == NULL) {
|
if (table == NULL) {
|
||||||
/* table allocation failed! */
|
/* table allocation failed! */
|
||||||
return 1;
|
return 1;
|
||||||
@@ -66,7 +67,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
/* test insertion */
|
/* test insertion */
|
||||||
for (i = 0; i < nkeys; i++) {
|
for (i = 0; i < nkeys; i++) {
|
||||||
err = hash_add(table, keys[i], (void*)values[i]);
|
err = hash_add(table, keys[i], xmpp_strdup(ctx, values[i]));
|
||||||
if (err) return err;
|
if (err) return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
/* test replacing old values */
|
/* test replacing old values */
|
||||||
for (i = 0; i < nkeys; i++) {
|
for (i = 0; i < nkeys; i++) {
|
||||||
err = hash_add(table, keys[0], (void*)values[i]);
|
err = hash_add(table, keys[0], xmpp_strdup(ctx, values[i]));
|
||||||
if (err) return err;
|
if (err) return err;
|
||||||
if (hash_num_keys(table) != nkeys) return 1;
|
if (hash_num_keys(table) != nkeys) return 1;
|
||||||
result = hash_get(table, keys[0]);
|
result = hash_get(table, keys[0]);
|
||||||
@@ -86,7 +87,7 @@ int main(int argc, char **argv)
|
|||||||
if (strcmp(result, values[i]) != 0) return 1;
|
if (strcmp(result, values[i]) != 0) return 1;
|
||||||
}
|
}
|
||||||
/* restore value for the 1st key */
|
/* restore value for the 1st key */
|
||||||
hash_add(table, keys[0], (void*)values[0]);
|
hash_add(table, keys[0], xmpp_strdup(ctx, values[0]));
|
||||||
|
|
||||||
/* test cloning */
|
/* test cloning */
|
||||||
clone = hash_clone(table);
|
clone = hash_clone(table);
|
||||||
|
|||||||
Reference in New Issue
Block a user