Closed stevefan1999-personal closed 6 years ago
So __KHASH_TYPE still stores keys references 🤦♂️
Workaround: duplicate the same string and hope that you could iterate the key list and free them.
static void set_var(const char *name, double val) {
printf("set %s to %g\n", name, val);
int ret;
khiter_t k = kh_put(var, h, strdup(name), &ret);
kh_value(h, k) = val;
}
But I still think this is a problem, it is very prominent in my little calculator example, that the identifiers as represented as an AST string but the AST node must be freed after evaluation, which in turns erases the duplicated AST string, and the reference of the string in the hash map is immediately invalid afterward. Now I 'reduplicate' another identifier string, but that means extra memory is used. This is a big headache...
Khash simply keeps pointers. You are responsible for the memory these pointers point to.
identifiers as represented as an AST string but the AST node must be freed after evaluation, which in turns erases the duplicated AST string, and the reference of the string in the hash map is immediately invalid afterward.
I don't understand. Once strings are duplicated, they will be there before you explicitly free them.
Reproduction code:
Result:
If I turned on optimization: