[erlang-questions] When to release resources in NIF?

Attila Rajmund Nohl attila.r.nohl@REDACTED
Fri Mar 21 18:15:12 CET 2014


Hello!

I have a NIF module that uses a locks to protect a common resource.
The locks are created using enif_rwlock_create in the load function
and destroyed in the unload function. When I first load the Erlang
module, the locks are initialized and everything is OK. When I compile
the module in the same VM the first time, everything is still OK. When
I compile it the second time, the old version of the module is
unloaded, so unload is called. At this point I'm in trouble, because
the unload function destroys the lock.

I'm not quite sure how to correctly handle this situation. Somehow I
should keep (or destroy/create) the locks in the upgrade step, but
after the second compilation the locks are already deleted by unload.
I could add a flag to the private data stating that the locks were
already destroyed or not, but it feels hackish. What is the correct
way?



More information about the erlang-questions mailing list