Fix a bug introduced by the last change to mono_marshal_get_virtual_stelemref ()...
[mono.git] / mono / metadata / image.c
index 2cc90233f763338ea436238aec3274f134bdb605..782ce36d7e3281455c985572b4c670efa43d18b8 100644 (file)
@@ -60,12 +60,11 @@ static CRITICAL_SECTION images_mutex;
 
 typedef struct ImageUnloadHook ImageUnloadHook;
 struct ImageUnloadHook {
-       ImageUnloadHook *next;
        MonoImageUnloadFunc func;
        gpointer user_data;
 };
 
-ImageUnloadHook *image_unload_hook = NULL;
+GSList *image_unload_hooks;
 
 void
 mono_install_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
@@ -77,16 +76,35 @@ mono_install_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
        hook = g_new0 (ImageUnloadHook, 1);
        hook->func = func;
        hook->user_data = user_data;
-       hook->next = image_unload_hook;
-       image_unload_hook = hook;
+       image_unload_hooks = g_slist_prepend (image_unload_hooks, hook);
+}
+
+void
+mono_remove_image_unload_hook (MonoImageUnloadFunc func, gpointer user_data)
+{
+       GSList *l;
+       ImageUnloadHook *hook;
+
+       for (l = image_unload_hooks; l; l = l->next) {
+               hook = l->data;
+
+               if (hook->func == func && hook->user_data == user_data) {
+                       g_free (hook);
+                       image_unload_hooks = g_slist_delete_link (image_unload_hooks, l);
+                       break;
+               }
+       }
 }
 
 static void
 mono_image_invoke_unload_hook (MonoImage *image)
 {
+       GSList *l;
        ImageUnloadHook *hook;
 
-       for (hook = image_unload_hook; hook; hook = hook->next) {
+       for (l = image_unload_hooks; l; l = l->next) {
+               hook = l->data;
+
                hook->func (image, hook->user_data);
        }
 }
@@ -564,7 +582,7 @@ mono_image_load_module (MonoImage *image, int idx)
        GList *list_iter, *valid_modules = NULL;
        MonoImageOpenStatus status;
 
-       if ((image->module_count == 0) || (idx > image->module_count))
+       if ((image->module_count == 0) || (idx > image->module_count || idx <= 0))
                return NULL;
        if (image->modules_loaded [idx - 1])
                return image->modules [idx - 1];
@@ -1236,8 +1254,12 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
                        if (status) {
                                if (last_error == ERROR_BAD_EXE_FORMAT || last_error == STATUS_INVALID_IMAGE_FORMAT)
                                        *status = MONO_IMAGE_IMAGE_INVALID;
-                               else
-                                       *status = MONO_IMAGE_ERROR_ERRNO;
+                               else {
+                                       if (last_error == ERROR_FILE_NOT_FOUND || last_error == ERROR_PATH_NOT_FOUND)
+                                               errno = ENOENT;
+                                       else
+                                               errno = 0;
+                               }
                        }
                        return NULL;
                }
@@ -1397,12 +1419,6 @@ free_mr_signatures (gpointer key, gpointer val, gpointer user_data)
 }
 */
 
-static void
-free_remoting_wrappers (gpointer key, gpointer val, gpointer user_data)
-{
-       g_free (val);
-}
-
 static void
 free_array_cache_entry (gpointer key, gpointer val, gpointer user_data)
 {
@@ -1510,7 +1526,7 @@ mono_image_close_except_pools (MonoImage *image)
                int i;
 
                for (i = 0; i < t->rows; i++) {
-                       if (image->references [i]) {
+                       if (image->references [i] && image->references [i] != REFERENCE_MISSING) {
                                if (!mono_assembly_close_except_image_pools (image->references [i]))
                                        image->references [i] = NULL;
                        }
@@ -1585,8 +1601,7 @@ mono_image_close_except_pools (MonoImage *image)
        free_hash (image->delegate_end_invoke_cache);
        free_hash (image->delegate_invoke_cache);
        free_hash (image->delegate_abstract_invoke_cache);
-       if (image->remoting_invoke_cache)
-               g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
+       free_hash (image->delegate_bound_static_invoke_cache);
        free_hash (image->remoting_invoke_cache);
        free_hash (image->runtime_invoke_cache);
        free_hash (image->runtime_invoke_direct_cache);
@@ -1674,7 +1689,7 @@ mono_image_close_finish (MonoImage *image)
                int i;
 
                for (i = 0; i < t->rows; i++) {
-                       if (image->references [i])
+                       if (image->references [i] && image->references [i] != REFERENCE_MISSING)
                                mono_assembly_close_finish (image->references [i]);
                }