Test issue for reference tracking in Objective-C (#56441)
authorAaron Robinson <arobins@microsoft.com>
Fri, 30 Jul 2021 02:55:40 +0000 (19:55 -0700)
committerGitHub <noreply@github.com>
Fri, 30 Jul 2021 02:55:40 +0000 (02:55 +0000)
* Test issue for reference count in Objective-C

* Review feedback.

src/tests/Interop/ObjectiveC/ObjectiveCMarshalAPI/NativeObjCMarshalTests.cpp
src/tests/Interop/ObjectiveC/ObjectiveCMarshalAPI/ObjectiveCMarshalAPI.csproj
src/tests/Interop/ObjectiveC/ObjectiveCMarshalAPI/Program.cs

index 5b6c16b..1062d5a 100644 (file)
@@ -47,13 +47,9 @@ namespace
             ::abort();
         }
 
-
         assert(mem != nullptr);
         auto cxt = (Contract*)mem;
 
-        cxt->RefCountDown--;
-        cxt->RefCountUp++;
-
         if (cxt->RefCountDown == 0)
         {
             // No more references
@@ -61,6 +57,8 @@ namespace
         }
         else
         {
+            cxt->RefCountDown--;
+            cxt->RefCountUp++;
             return 1;
         }
     }
index 7ae2616..9a81c00 100644 (file)
@@ -4,8 +4,6 @@
     <!-- Test unsupported outside of OSX -->
     <CLRTestTargetUnsupported Condition="'$(TargetsOSX)' != 'true'">true</CLRTestTargetUnsupported>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-    <!-- Disable under GCStress. Tracking: https://github.com/dotnet/runtime/issues/53359 -->
-    <GCStressIncompatible>true</GCStressIncompatible>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="Program.cs" />
index c49f94e..9072890 100644 (file)
@@ -139,7 +139,7 @@ namespace ObjectiveCMarshalAPI
             ObjectiveCMarshal.Initialize(beginEndCallback, isReferencedCallback, trackedObjectEnteredFinalization, OnUnhandledExceptionPropagationHandler);
         }
 
-        static GCHandle AllocAndTrackObject<T>() where T : Base, new()
+        static GCHandle AllocAndTrackObject<T>(uint count) where T : Base, new()
         {
             var obj = new T();
             GCHandle h = ObjectiveCMarshal.CreateReferenceTrackingHandle(obj, out Span<IntPtr> s);
@@ -147,9 +147,9 @@ namespace ObjectiveCMarshalAPI
             // Validate contract length for tagged memory.
             Assert.AreEqual(2, s.Length);
 
-            // Make the "is referenced" callback run a few times.
+            // Make the "is referenced" callback run at least 'count' number of times.
             fixed (void* p = s)
-                obj.SetContractMemory((IntPtr)p, count: 3);
+                obj.SetContractMemory((IntPtr)p, count);
             return h;
         }
 
@@ -188,24 +188,27 @@ namespace ObjectiveCMarshalAPI
                     ObjectiveCMarshal.CreateReferenceTrackingHandle(new AttributedNoFinalizer(), out _);
                 });
 
+            // Provide the minimum number of times the reference callback should run.
+            // See IsRefCb() in NativeObjCMarshalTests.cpp for usage logic.
+            const uint callbackCount = 3;
             {
-                GCHandle h = AllocAndTrackObject<Base>();
+                GCHandle h = AllocAndTrackObject<Base>(callbackCount);
                 handles.Add(h);
                 Validate_AllocAndFreeAnotherHandle<Base>(h);
             }
             {
-                GCHandle h = AllocAndTrackObject<Derived>();
+                GCHandle h = AllocAndTrackObject<Derived>(callbackCount);
                 handles.Add(h);
                 Validate_AllocAndFreeAnotherHandle<Derived>(h);
             }
             {
-                GCHandle h = AllocAndTrackObject<DerivedWithFinalizer>();
+                GCHandle h = AllocAndTrackObject<DerivedWithFinalizer>(callbackCount);
                 handles.Add(h);
                 Validate_AllocAndFreeAnotherHandle<DerivedWithFinalizer>(h);
             }
 
             // Trigger the GC
-            for (int i = 0; i < 5; ++i)
+            for (int i = 0; i < (callbackCount + 2); ++i)
             {
                 GC.Collect();
                 GC.WaitForPendingFinalizers();