src/module.c
changeset 111 5a1ebffca81a
parent 110 43e9a7984955
child 119 64f50072db9e
--- a/src/module.c	Thu Apr 02 02:25:35 2009 +0300
+++ b/src/module.c	Thu Apr 02 02:55:32 2009 +0300
@@ -238,8 +238,10 @@
         goto error;
 
     // ok
-    if (module_ptr)
+    if (module_ptr) {
+        module->refcount++;
         *module_ptr = module;
+    }
 
     return SUCCESS;
 
@@ -304,8 +306,9 @@
 {
     err_t err;
     
-    // sanity-check
-    assert(!module->unloading);
+    // wrong state
+    if (module->unloading)
+        return ERR_MODULE_STATE;
 
     // remove from modules list
     TAILQ_REMOVE(&module->modules->list, module, modules_list);
@@ -315,8 +318,12 @@
 
     // invoke the unload func, passing it our reference
     // note that the module might not exist anymore after calling this
-    if ((err = module->desc->unload(module->ctx, module)))
+    if ((err = module->desc->unload(module->ctx, module))) {
+        // mark it as "unloaded"
+        module_unloaded(module);
+
         return err;
+    }
 
     // ok
     return SUCCESS;
@@ -364,7 +371,7 @@
         TAILQ_REMOVE(&module->modules->list, module, modules_list);
 
     // run the destroy hook
-    if (module->desc->destroy)
+    if (module->desc && module->desc->destroy)
         module->desc->destroy(module->ctx);
 
     // unload the dl handle