diff --git a/src/CoreFoundation/CFAllocator.cs b/src/CoreFoundation/CFAllocator.cs index fae4936f5c64..e0ac221492de 100644 --- a/src/CoreFoundation/CFAllocator.cs +++ b/src/CoreFoundation/CFAllocator.cs @@ -84,6 +84,7 @@ public static CFAllocator MallocZone { } } + // Internal only: to get the null handle, use 'CFAllocator.null_ptr' instead. /// To be added. /// To be added. /// To be added. diff --git a/src/CoreMedia/CMBlockBuffer.cs b/src/CoreMedia/CMBlockBuffer.cs index f44278db6066..5c99eea884d5 100644 --- a/src/CoreMedia/CMBlockBuffer.cs +++ b/src/CoreMedia/CMBlockBuffer.cs @@ -260,7 +260,7 @@ public bool IsEmpty { public static CMBlockBuffer? FromMemoryBlock (IntPtr memoryBlock, nuint blockLength, CMCustomBlockAllocator? customBlockSource, nuint offsetToData, nuint dataLength, CMBlockBufferFlags flags, out CMBlockBufferError error) { - var blockAllocator = memoryBlock == IntPtr.Zero ? NativeHandle.Zero : CFAllocator.Null.Handle; + var blockAllocator = memoryBlock == IntPtr.Zero ? NativeHandle.Zero : (NativeHandle) CFAllocator.null_ptr; IntPtr buffer; unsafe { if (customBlockSource is null) { @@ -339,7 +339,7 @@ public bool IsEmpty { public CMBlockBufferError AppendMemoryBlock (IntPtr memoryBlock, nuint blockLength, CMCustomBlockAllocator customBlockSource, nuint offsetToData, nuint dataLength, CMBlockBufferFlags flags) { - var blockAllocator = memoryBlock == IntPtr.Zero ? NativeHandle.Zero : CFAllocator.Null.Handle; + var blockAllocator = memoryBlock == IntPtr.Zero ? NativeHandle.Zero : (NativeHandle) CFAllocator.null_ptr; unsafe { if (customBlockSource is null) { return CMBlockBufferAppendMemoryBlock (GetCheckedHandle (), memoryBlock, blockLength, blockAllocator, null, offsetToData, dataLength, flags); diff --git a/src/Foundation/NSArray.cs b/src/Foundation/NSArray.cs index edc99558ebf8..c39fc9fe72a4 100644 --- a/src/Foundation/NSArray.cs +++ b/src/Foundation/NSArray.cs @@ -333,7 +333,7 @@ static NSArray FromNativeObjectsImpl (T? []? items, nint count, bool allowNul throw new ArgumentNullException ($"{nameof (items)}[{i}]"); // The analyzer cannot deal with arrays, we manually keep alive the whole array below #pragma warning disable RBI0014 - IntPtr h = item is null ? NSNull.Null.Handle : item.Handle; + IntPtr h = item is null ? NSNull.NullHandle : item.Handle; handles [i] = h; #pragma warning restore RBI0014 } @@ -672,7 +672,7 @@ static T UnsafeGetItem (NativeHandle handle, nuint index) where T : class, IN // A native code could return NSArray with NSNull.Null elements // and they should be valid for things like T : NSDate so we handle // them as just null values inside the array - if (val == NSNull.Null.Handle) + if (val == NSNull.NullHandle) return null; return Runtime.GetINativeObject (val, false); @@ -684,7 +684,7 @@ static object UnsafeGetItem (NativeHandle handle, nuint index, Type type) // A native code could return NSArray with NSNull.Null elements // and they should be valid for things like T : NSDate so we handle // them as just null values inside the array - if (val == NSNull.Null.Handle) + if (val == NSNull.NullHandle) return null; return Runtime.GetINativeObject (val, false, type); diff --git a/src/Foundation/NSArray_1.cs b/src/Foundation/NSArray_1.cs index 53526538c666..91f4c55b2c37 100644 --- a/src/Foundation/NSArray_1.cs +++ b/src/Foundation/NSArray_1.cs @@ -79,7 +79,7 @@ static public NSArray FromNSObjects (int count, params TKey [] items) var item = items [i]; // The analyzer cannot deal with arrays, we manually keep alive the whole array below #pragma warning disable RBI0014 - IntPtr h = item is null ? NSNull.Null.Handle : item.Handle; + IntPtr h = item is null ? NSNull.NullHandle : item.Handle; Marshal.WriteIntPtr (buf, (int) (i * IntPtr.Size), h); #pragma warning restore RBI0014 } diff --git a/src/Foundation/NSNull.cs b/src/Foundation/NSNull.cs index a705856ca502..bf8f78863b90 100644 --- a/src/Foundation/NSNull.cs +++ b/src/Foundation/NSNull.cs @@ -20,5 +20,14 @@ static public NSNull Null { return _null; } } + + static IntPtr nullHandle; + internal static IntPtr NullHandle { + get { + if (nullHandle == IntPtr.Zero) + nullHandle = _NullHandle; + return nullHandle; + } + } } } diff --git a/src/ObjCRuntime/BindAs.cs b/src/ObjCRuntime/BindAs.cs index 8e5e5201246c..f0618996797c 100644 --- a/src/ObjCRuntime/BindAs.cs +++ b/src/ObjCRuntime/BindAs.cs @@ -70,7 +70,7 @@ unsafe static IntPtr ConvertManagedArrayToNSArray2 (T []? array, delegate* for (nint i = 0; i < count; i++) { var item = convert2 (convert1 (array [i])); if (item == IntPtr.Zero) - item = NSNull.Null.Handle; + item = NSNull.NullHandle; ptrs [i] = item; } fixed (void* ptr = ptrs) { diff --git a/src/SceneKit/SCNSkinner.cs b/src/SceneKit/SCNSkinner.cs index 6eb9fadb0cb7..00709af260ed 100644 --- a/src/SceneKit/SCNSkinner.cs +++ b/src/SceneKit/SCNSkinner.cs @@ -36,7 +36,7 @@ static NSArray ToNSArray (SCNMatrix4 []? items) for (nint i = 0; i < count; i++) { var item = NSValue.FromSCNMatrix4 (items [i]); - var h = item?.Handle ?? NSNull.Null.Handle; + var h = item?.Handle ?? NSNull.NullHandle; Marshal.WriteIntPtr (buf, (int) (i * IntPtr.Size), h); } diff --git a/src/foundation.cs b/src/foundation.cs index 0d3c66dd191f..85df5c4e6ec4 100644 --- a/src/foundation.cs +++ b/src/foundation.cs @@ -6682,6 +6682,10 @@ interface NSNull : NSSecureCoding, NSCopying [Export ("null"), Static] [Internal] NSNull _Null { get; } + + [Export ("null"), Static] + [Internal] + IntPtr _NullHandle { get; } } [MacCatalyst (13, 1)] diff --git a/tests/cecil-tests/HandleSafety.KnownFailures.cs b/tests/cecil-tests/HandleSafety.KnownFailures.cs index 18df7f337745..c8f5e6514460 100644 --- a/tests/cecil-tests/HandleSafety.KnownFailures.cs +++ b/tests/cecil-tests/HandleSafety.KnownFailures.cs @@ -46,8 +46,6 @@ public partial class HandleSafetyTest { "CoreImage.CIAutoAdjustmentFilterOptions.ToDictionary ()", "CoreImage.CIImage.FromCGImage (CoreGraphics.CGImage, CoreGraphics.CGColorSpace)", "CoreImage.CISamplerOptions.ToDictionary ()", - "CoreMedia.CMBlockBuffer.AppendMemoryBlock (System.IntPtr, System.UIntPtr, CoreMedia.CMCustomBlockAllocator, System.UIntPtr, System.UIntPtr, CoreMedia.CMBlockBufferFlags)", - "CoreMedia.CMBlockBuffer.FromMemoryBlock (System.IntPtr, System.UIntPtr, CoreMedia.CMCustomBlockAllocator, System.UIntPtr, System.UIntPtr, CoreMedia.CMBlockBufferFlags, CoreMedia.CMBlockBufferError&)", "CoreMidi.MidiCIDeviceInfo..ctor (CoreMidi.MidiEndpoint, Foundation.NSData, Foundation.NSData, Foundation.NSData, Foundation.NSData)", "CoreMidi.MidiDevice.Add (System.String, System.Boolean, System.UIntPtr, System.UIntPtr, CoreMidi.MidiEntity)", "CoreMidi.MidiDeviceList.Add (CoreMidi.MidiDevice)", @@ -107,8 +105,6 @@ public partial class HandleSafetyTest { "Foundation.DictionaryContainerHelper.GetHandle (Foundation.DictionaryContainer)", "Foundation.NSArray.FromNativeObjectsImpl`1 (T[], System.IntPtr, System.Boolean)", "Foundation.NSArray.FromNSObjects (System.Collections.Generic.IList`1)", - "Foundation.NSArray.UnsafeGetItem (ObjCRuntime.NativeHandle, System.UIntPtr, System.Type)", - "Foundation.NSArray.UnsafeGetItem`1 (ObjCRuntime.NativeHandle, System.UIntPtr)", "Foundation.NSArray`1.FromNSObjects`0 (System.Int32, TKey[])", "Foundation.NSDecimal.ToString ()", "Foundation.NSFastEnumerator`1.Fetch ()",