Fix resource destructors
Apparently the resource name is quite important. The old code was
attempting to free all hashes with the iterator destructor which also
had a bug that masked the underlying issue. This fixes the wrong
destructor issue as well as the iterator destructor bug.
diff --git a/c_src/khash.c b/c_src/khash.c
index 038c81c..d206f5e 100644
--- a/c_src/khash.c
+++ b/c_src/khash.c
@@ -493,7 +493,7 @@
khash_iter_free(ErlNifEnv* env, void* obj)
{
khash_iter_t* iter = (khash_iter_t*) obj;
- enif_release_resource(iter);
+ enif_release_resource(iter->khash);
}
@@ -538,7 +538,6 @@
static int
load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
{
- const char* mod = "khash";
int flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
ErlNifResourceType* res;
@@ -547,13 +546,15 @@
return 1;
}
- res = enif_open_resource_type(env, mod, "", khash_free, flags, NULL);
+ res = enif_open_resource_type(
+ env, NULL, "khash", khash_free, flags, NULL);
if(res == NULL) {
return 1;
}
new_priv->res_hash = res;
- res = enif_open_resource_type(env, mod, "", khash_iter_free, flags, NULL);
+ res = enif_open_resource_type(
+ env, NULL, "khash_iter", khash_iter_free, flags, NULL);
if(res == NULL) {
return 1;
}