diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
index 0b268cc6e12bee..5a94a338c06588 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
@@ -449,6 +449,15 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
return GetMethodTable(obj)->HasComponentSize;
}
+ // Returns true iff the type of the object requires finalization,
+ // which includes a finalizer inherited from a base type.
+ internal static unsafe bool ObjectHasFinalizer(object obj)
+ {
+ bool hasFinalizer = GetMethodTable(obj)->HasFinalizer;
+ GC.KeepAlive(obj); // Keep MethodTable alive
+ return hasFinalizer;
+ }
+
///
/// Boxes a given value using an input to determine its type.
///
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
index 487c460e1c1b23..93d0d56a4f94b0 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
@@ -213,6 +213,13 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
return GetMethodTable(obj)->HasComponentSize;
}
+ // Returns true iff the type of the object requires finalization,
+ // which includes a finalizer inherited from a base type.
+ internal static unsafe bool ObjectHasFinalizer(object obj)
+ {
+ return GetMethodTable(obj)->IsFinalizable;
+ }
+
public static void PrepareMethod(RuntimeMethodHandle method)
{
if (method.Value == IntPtr.Zero)
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
index 7bc461cef8d54c..f6fa0b09c2aaf3 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/TrackerObjectManager.NativeAot.cs
@@ -186,7 +186,8 @@ internal static void DetachNonPromotedObjects()
ReferenceTrackerNativeObjectWrapper? nativeObjectWrapper = Unsafe.As(weakNativeObjectWrapperHandle.Target);
if (nativeObjectWrapper != null &&
nativeObjectWrapper.TrackerObject != IntPtr.Zero &&
- !RuntimeImports.RhIsPromoted(nativeObjectWrapper.ProxyHandle.Target))
+ nativeObjectWrapper.ProxyHandle.TryGetTarget(out object? proxyTarget) &&
+ !RuntimeImports.RhIsPromoted(proxyTarget))
{
// Notify the wrapper it was not promoted and is being collected.
BeforeWrapperFinalized(nativeObjectWrapper.TrackerObject);
@@ -205,9 +206,9 @@ internal static unsafe class FindReferenceTargetsCallback
internal ref struct Instance
{
private readonly IntPtr _vtable; // First field is IUnknown based vtable.
- public GCHandle RootObject;
+ public WeakGCHandle