MonoObject *o, *o2;
o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
+#ifndef HAVE_SGEN_GC
mono_domain_lock (o->vtable->domain);
o2 = g_hash_table_lookup (o->vtable->domain->finalizable_objects_hash, o);
if (!o2)
/* Already finalized somehow */
return;
+#endif
/* make sure the finalizer is not called again if the object is resurrected */
object_register_finalizer (obj, NULL);
mono_domain_unlock (obj->vtable->domain);
GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
+#elif defined(HAVE_SGEN_GC)
+ mono_gc_register_for_finalization (obj, callback);
#endif
}
{
MONO_ARCH_SAVE_REGS;
+ /* delegates have no finalizers, but we register them to deal with the
+ * unmanaged->managed trampoline. We don't let the user suppress it
+ * otherwise we'd leak it.
+ */
+ if (obj->vtable->klass->delegate)
+ return;
+
object_register_finalizer (obj, NULL);
}
memcpy (domain_ids, handles->domain_ids, sizeof (guint16) * handles->size);
for (i = 0; i < handles->size; ++i) {
MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
- mono_gc_weak_link_remove (&(handles->entries [i]));
+ if (handles->entries [i])
+ mono_gc_weak_link_remove (&(handles->entries [i]));
/*g_print ("reg/unreg entry %d of type %d at %p to object %p (%p), was: %p\n", i, handles->type, &(entries [i]), obj, entries [i], handles->entries [i]);*/
if (obj) {
mono_gc_weak_link_add (&(entries [i]), obj);
lock_handles (handles);
if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
if (handles->type <= HANDLE_WEAK_TRACK) {
- mono_gc_weak_link_remove (&handles->entries [slot]);
+ if (handles->entries [slot])
+ mono_gc_weak_link_remove (&handles->entries [slot]);
if (obj)
mono_gc_weak_link_add (&handles->entries [slot], obj);
} else {
return;
lock_handles (handles);
if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
- if (handles->type <= HANDLE_WEAK_TRACK)
- mono_gc_weak_link_remove (&handles->entries [slot]);
- else
+ if (handles->type <= HANDLE_WEAK_TRACK) {
+ if (handles->entries [slot])
+ mono_gc_weak_link_remove (&handles->entries [slot]);
+ } else {
handles->entries [slot] = NULL;
+ }
handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
} else {
/* print a warning? */
unlock_handles (handles);
}
+/**
+ * mono_gchandle_free_domain:
+ * @domain: domain that is unloading
+ *
+ * Function used internally to cleanup any GC handle for objects belonging
+ * to the specified domain during appdomain unload.
+ */
+void
+mono_gchandle_free_domain (MonoDomain *domain)
+{
+ guint type;
+
+ for (type = 0; type < 3; ++type) {
+ guint slot;
+ HandleData *handles = &gc_handles [type];
+ lock_handles (handles);
+ for (slot = 0; slot < handles->size; ++slot) {
+ if (!(handles->bitmap [slot / 32] & (1 << (slot % 32))))
+ continue;
+ if (type <= HANDLE_WEAK_TRACK) {
+ if (domain->domain_id == handles->domain_ids [slot]) {
+ handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
+ if (handles->entries [slot])
+ mono_gc_weak_link_remove (&handles->entries [slot]);
+ }
+ } else {
+ if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
+ handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
+ handles->entries [slot] = NULL;
+ }
+ }
+ }
+ unlock_handles (handles);
+ }
+
+}
+
#ifndef HAVE_NULL_GC
static HANDLE finalizer_event;
static void
finalize_domain_objects (DomainFinalizationReq *req)
{
- int i;
- GPtrArray *objs;
MonoDomain *domain = req->domain;
-
+
+#ifdef HAVE_BOEHM_GC
while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
+ int i;
+ GPtrArray *objs;
/*
* Since the domain is unloading, nobody is allowed to put
* new entries into the hash table. But finalize_object might
g_ptr_array_free (objs, TRUE);
}
+#elif defined(HAVE_SGEN_GC)
+#define NUM_FOBJECTS 64
+ MonoObject *to_finalize [NUM_FOBJECTS];
+ int count;
+ while ((count = mono_gc_finalizers_for_domain (domain, to_finalize, NUM_FOBJECTS))) {
+ int i;
+ for (i = 0; i < count; ++i) {
+ run_finalize (to_finalize [i], 0);
+ }
+ }
+#endif
/* Process finalizers which are already in the queue */
mono_gc_invoke_finalizers ();
/* Wait to be notified that there's at least one
* finaliser to run
*/
- WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
+ /* Use alertable=FALSE since we will be asked to exit using the event too */
+ WaitForSingleObjectEx (finalizer_event, INFINITE, FALSE);
if (domains_to_finalize) {
mono_finalizer_lock ();
}
#endif
+
+ DeleteCriticalSection (&handle_section);
+ DeleteCriticalSection (&allocator_section);
+ DeleteCriticalSection (&finalizer_mutex);
}
#else