@@ -81,6 +81,8 @@ static gboolean finalizer_thread_exited;
8181static MonoCoopCond exited_cond ;
8282
8383static MonoInternalThread * gc_thread ;
84+
85+ static MonoMethod * finalize_method ;
8486#ifndef HOST_WASM
8587static RuntimeInvokeFunction finalize_runtime_invoke ;
8688#endif
@@ -289,11 +291,13 @@ mono_gc_run_finalize (void *obj, void *data)
289291 if (mono_log_finalizers )
290292 g_log ("mono-gc-finalizers" , G_LOG_LEVEL_MESSAGE , "<%s at %p> Compiling finalizer." , o_name , o );
291293
294+ if (!finalize_method ) {
295+ finalize_method = mono_class_get_method_from_name_checked (mono_defaults .object_class , "GuardedFinalize" , 0 , 0 , error );
296+ mono_error_assert_ok (error );
297+ }
298+
292299#ifndef HOST_WASM
293300 if (!finalize_runtime_invoke ) {
294- MonoMethod * finalize_method = mono_class_get_method_from_name_checked (mono_defaults .object_class , "GuardedFinalize" , 0 , 0 , error );
295-
296- mono_error_assert_ok (error );
297301 MonoMethod * invoke = mono_marshal_get_runtime_invoke_full (finalize_method , FALSE, TRUE);
298302
299303 finalize_runtime_invoke = (RuntimeInvokeFunction )mono_compile_method_checked (invoke , error );
@@ -317,12 +321,9 @@ mono_gc_run_finalize (void *obj, void *data)
317321 MONO_PROFILER_RAISE (gc_finalizing_object , (o ));
318322
319323#ifdef HOST_WASM
320- MonoMethod * finalizer = mono_class_get_method_from_name_checked (mono_defaults .object_class , "GuardedFinalize" , 0 , 0 , error );
321- if (finalizer ) { // null finalizers work fine when using the vcall invoke as Object has an empty one
322- gpointer params [1 ];
323- params [0 ] = NULL ;
324- mono_runtime_try_invoke (finalizer , o , params , & exc , error );
325- }
324+ gpointer params [1 ];
325+ params [0 ] = NULL ;
326+ mono_runtime_try_invoke (finalize_method , o , params , & exc , error );
326327#else
327328 runtime_invoke (o , NULL , & exc , NULL );
328329#endif
0 commit comments