Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/AddressBook/ABMultiValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,17 @@ static Exception CreateNotSupportedException ()
public T Value {
get {
AssertValid ();
return self.toManaged (ABMultiValue.CopyValueAtIndex (self.Handle, index));
var rv = self.toManaged (ABMultiValue.CopyValueAtIndex (self.Handle, index));
GC.KeepAlive (self);
return rv;
}
set {
if (IsReadOnly)
throw CreateNotSupportedException ();
AssertValid ();
if (ABMultiValue.ReplaceValueAtIndex (self.Handle, ToIntPtr (value), index) == 0)
var rv = ABMultiValue.ReplaceValueAtIndex (self.Handle, ToIntPtr (value), index);
GC.KeepAlive (self);
if (rv == 0)
throw new ArgumentException ("Value cannot be set");
}
}
Expand All @@ -212,14 +216,17 @@ public T Value {
public NSString? Label {
get {
AssertValid ();
return Runtime.GetNSObject<NSString> (ABMultiValue.CopyLabelAtIndex (self.Handle, index), true);
var rv = Runtime.GetNSObject<NSString> (ABMultiValue.CopyLabelAtIndex (self.Handle, index), true);
GC.KeepAlive (self);
return rv;
}
set {
if (IsReadOnly)
throw CreateNotSupportedException ();
AssertValid ();
ABMultiValue.ReplaceLabelAtIndex (self.Handle, value.GetHandle (), index);
GC.KeepAlive (value);
GC.KeepAlive (self);
}
}

Expand All @@ -243,7 +250,9 @@ public NSString? Label {
public int Identifier {
get {
AssertValid ();
return ABMultiValue.GetIdentifierAtIndex (self.Handle, index);
var rv = ABMultiValue.GetIdentifierAtIndex (self.Handle, index);
GC.KeepAlive (self);
return rv;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/AudioToolbox/MusicTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,10 @@ internal MusicTrack (MusicSequence sequence, IntPtr handle, bool owns)
protected override void Dispose (bool disposing)
{
if (Handle != IntPtr.Zero && Owns) {
if (sequence is not null)
if (sequence is not null) {
MusicSequenceDisposeTrack (sequence.Handle, Handle);
GC.KeepAlive (sequence);
}
}
sequence = null;
base.Dispose (disposing);
Expand Down
7 changes: 5 additions & 2 deletions src/AudioUnit/AudioUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -625,10 +625,13 @@ public AudioStreamBasicDescription GetAudioFormat (AudioUnitScopeType scope, uin
/// <remarks>To be added.</remarks>
public AudioUnitStatus SetClassInfo (ClassInfoDictionary preset, AudioUnitScopeType scope = AudioUnitScopeType.Global, uint audioUnitElement = 0)
{
var ptr = preset.Dictionary.Handle;
var dictionary = preset.Dictionary;
var ptr = dictionary.Handle;
unsafe {
return AudioUnitSetProperty (Handle, AudioUnitPropertyIDType.ClassInfo, scope, audioUnitElement,
var rv = AudioUnitSetProperty (Handle, AudioUnitPropertyIDType.ClassInfo, scope, audioUnitElement,
&ptr, IntPtr.Size);
GC.KeepAlive (dictionary);
return rv;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/CoreFoundation/CFArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ public static unsafe NativeHandle Create (params INativeObject [] values)
var _values = c <= 256 ? stackalloc IntPtr [c] : new IntPtr [c];
for (int i = 0; i < c; ++i)
_values [i] = values [i].Handle;
IntPtr rv;
fixed (IntPtr* pv = _values)
return CFArrayCreate (IntPtr.Zero, (IntPtr) pv, c, kCFTypeArrayCallbacks_ptr);
rv = CFArrayCreate (IntPtr.Zero, (IntPtr) pv, c, kCFTypeArrayCallbacks_ptr);
GC.KeepAlive (values);
return rv;
}

/// <summary>Create a <see cref="CFArray" /> from an array of strings.</summary>
Expand Down
5 changes: 4 additions & 1 deletion src/CoreFoundation/CFDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ public static CFDictionary FromObjectsAndKeys (INativeObject [] objects, INative
v [i] = objects [i].Handle;
}

return new CFDictionary (CFDictionaryCreate (IntPtr.Zero, k, v, k.Length, KeyCallbacks, ValueCallbacks), true);
var rv = new CFDictionary (CFDictionaryCreate (IntPtr.Zero, k, v, k.Length, KeyCallbacks, ValueCallbacks), true);
GC.KeepAlive (keys);
GC.KeepAlive (objects);
return rv;
}

[DllImport (Constants.CoreFoundationLibrary)]
Expand Down
4 changes: 4 additions & 0 deletions src/CoreFoundation/CFPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public static void SetAppValue (string key, object? value, NSString applicationI
} else if (value is string) {
using (var valueStr = new CFString ((string) value)) {
CFPreferencesSetAppValue (cfKey.Handle, valueStr.Handle, applicationId.Handle);
GC.KeepAlive (cfKey);
GC.KeepAlive (applicationId);
}

Expand All @@ -164,6 +165,7 @@ value is NSArray || value is CFArray ||
value is NSDictionary || value is CFDictionary ||
value is NSNumber || value is CFBoolean) {
CFPreferencesSetAppValue (cfKey.Handle, ((INativeObject) value).Handle, applicationId.Handle);
GC.KeepAlive (cfKey);
GC.KeepAlive (value);
GC.KeepAlive (applicationId);
return;
Expand All @@ -173,6 +175,8 @@ value is NSDictionary || value is CFDictionary ||
if (nsnumber is not null) {
using (nsnumber) {
CFPreferencesSetAppValue (cfKey.Handle, nsnumber.Handle, applicationId.Handle);
GC.KeepAlive (cfKey);
GC.KeepAlive (nsnumber);
GC.KeepAlive (applicationId);
}
return;
Expand Down
1 change: 1 addition & 0 deletions src/CoreGraphics/CGPDFDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public CGPDFDictionary GetInfo ()
public void SetOutline (CGPDFOutlineOptions? options)
{
CGPDFContextSetOutline (Handle, options.GetHandle ());
GC.KeepAlive (options);
}

[SupportedOSPlatform ("ios")]
Expand Down
8 changes: 6 additions & 2 deletions src/CoreMidi/MidiServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,9 @@ public class MidiDevice : MidiObject {
public int Add (string name, bool embedded, nuint numSourceEndpoints, nuint numDestinationEndpoints, MidiEntity newEntity)
{
using (NSString nsName = new NSString (name)) {
return MIDIDeviceAddEntity (GetCheckedHandle (), nsName.Handle, embedded ? (byte) 1 : (byte) 0, numSourceEndpoints, numDestinationEndpoints, newEntity.Handle);
var rv = MIDIDeviceAddEntity (GetCheckedHandle (), nsName.Handle, embedded ? (byte) 1 : (byte) 0, numSourceEndpoints, numDestinationEndpoints, newEntity.Handle);
GC.KeepAlive (newEntity);
return rv;
}
}

Expand Down Expand Up @@ -2542,7 +2544,9 @@ public nuint GetNumberOfDevices ()
/// <remarks>To be added.</remarks>
public int Add (MidiDevice device)
{
return MIDIDeviceListAddDevice (GetCheckedHandle (), device.Handle);
var rv = MIDIDeviceListAddDevice (GetCheckedHandle (), device.Handle);
GC.KeepAlive (device);
return rv;
}

internal override void DisposeHandle ()
Expand Down
5 changes: 4 additions & 1 deletion src/CoreServices/FSEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ public FSEventStream (FSEventStreamCreateOptions options)
context.Release = &FreeGCHandle;
}

var allocator = options.Allocator.GetHandle ();
var allocatorObject = options.Allocator;
var allocator = allocatorObject.GetHandle ();
var sinceWhenId = options.SinceWhenId ?? FSEvent.SinceNowId;
var latency = options.Latency.TotalSeconds;
var flags = options.Flags |= (FSEventStreamCreateFlags) 0x1 /* UseCFTypes */;
Expand All @@ -366,13 +367,15 @@ public FSEventStream (FSEventStreamCreateOptions options)
&context,
options.DeviceToWatch.Value,
pathsToWatch.Handle, sinceWhenId, latency, flags);
GC.KeepAlive (allocatorObject);
GC.KeepAlive (pathsToWatch);
} else {
handle = FSEventStreamCreate (
allocator,
&EventsCallback,
&context,
pathsToWatch.Handle, sinceWhenId, latency, flags);
GC.KeepAlive (allocatorObject);
GC.KeepAlive (pathsToWatch);
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/CoreText/CTFontCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ internal CTFontCollection (NativeHandle handle, bool owns)
/// <remarks>
/// </remarks>
public CTFontCollection (CTFontCollectionOptions? options)
: base (CTFontCollectionCreateFromAvailableFonts (options.GetHandle ()), true, true)
: base (CreateFromAvailableFonts (options), true, true)
{
}

static IntPtr CreateFromAvailableFonts (CTFontCollectionOptions? options)
{
var rv = CTFontCollectionCreateFromAvailableFonts (options.GetHandle ());
GC.KeepAlive (options);
return rv;
}

[DllImport (Constants.CoreTextLibrary)]
static extern IntPtr CTFontCollectionCreateWithFontDescriptors (IntPtr queryDescriptors, IntPtr options);
static IntPtr Create (CTFontDescriptor []? queryDescriptors, CTFontCollectionOptions? options)
Expand Down Expand Up @@ -201,6 +208,7 @@ public CTFontDescriptor [] GetMatchingFontDescriptors ()
public CTFontDescriptor [] GetMatchingFontDescriptors (CTFontCollectionOptions? options)
{
var cfArrayRef = CTFontCollectionCreateMatchingFontDescriptorsWithOptions (Handle, options.GetHandle ());
GC.KeepAlive (options);
if (cfArrayRef == IntPtr.Zero)
return Array.Empty<CTFontDescriptor> ();
return CFArray.ArrayFromHandleFunc (cfArrayRef, fd => new CTFontDescriptor (fd, false), true)!;
Expand Down
4 changes: 3 additions & 1 deletion src/CoreText/CTFramesetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ public CGSize SuggestFrameSize (NSRange stringRange, CTFrameAttributes? frameAtt
fitRange = default;
unsafe {
fixed (NSRange* fitRangePtr = &fitRange) {
return CTFramesetterSuggestFrameSizeWithConstraints (
var rv = CTFramesetterSuggestFrameSizeWithConstraints (
Handle, stringRange,
frameAttributes.GetHandle (),
constraints, fitRangePtr);
GC.KeepAlive (frameAttributes);
return rv;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/CoreText/CTTextTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public CTTextTab (CTTextAlignment alignment, double location)
public CTTextTab (CTTextAlignment alignment, double location, CTTextTabOptions? options)
: base (CTTextTabCreate (alignment, location, options.GetHandle ()), true, true)
{
GC.KeepAlive (options);
}
#endregion

Expand Down
1 change: 1 addition & 0 deletions src/CoreText/CTTypesetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public CTTypesetter (NSAttributedString value, CTTypesetterOptions? options)
: base (CTTypesetterCreateWithAttributedStringAndOptions (value.GetNonNullHandle (nameof (value)), options.GetHandle ()), true, true)
{
GC.KeepAlive (value);
GC.KeepAlive (options);
}
#endregion

Expand Down
15 changes: 12 additions & 3 deletions src/CoreVideo/CVImageBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ public CGColorSpace? ColorSpace {
[SupportedOSPlatform ("maccatalyst")]
public static int GetCodePoint (CVImageBufferYCbCrMatrix yCbCrMatrix)
{
return CVYCbCrMatrixGetIntegerCodePointForString (yCbCrMatrix.GetConstant ()!.Handle);
var constant = yCbCrMatrix.GetConstant ();
var rv = CVYCbCrMatrixGetIntegerCodePointForString (constant.GetHandle ());
GC.KeepAlive (constant);
return rv;
}

[SupportedOSPlatform ("ios")]
Expand All @@ -155,7 +158,10 @@ public static int GetCodePoint (CVImageBufferYCbCrMatrix yCbCrMatrix)
[SupportedOSPlatform ("maccatalyst")]
public static int GetCodePoint (CVImageBufferColorPrimaries color)
{
return CVColorPrimariesGetIntegerCodePointForString (color.GetConstant ()!.Handle);
var constant = color.GetConstant ();
var rv = CVColorPrimariesGetIntegerCodePointForString (constant.GetHandle ());
GC.KeepAlive (constant);
return rv;
}

[SupportedOSPlatform ("ios")]
Expand All @@ -175,7 +181,10 @@ public static int GetCodePoint (CVImageBufferColorPrimaries color)
[SupportedOSPlatform ("maccatalyst")]
public static int GetCodePoint (CVImageBufferTransferFunction function)
{
return CVTransferFunctionGetIntegerCodePointForString (function.GetConstant ()!.Handle);
var constant = function.GetConstant ();
var rv = CVTransferFunctionGetIntegerCodePointForString (constant.GetHandle ());
GC.KeepAlive (constant);
return rv;
}

[SupportedOSPlatform ("ios")]
Expand Down
2 changes: 2 additions & 0 deletions src/CoreVideo/CVPixelBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ static unsafe extern CVReturn CVPixelBufferCreateWithBytes (
DictionaryContainerHelper.GetHandle (pixelBufferAttributes),
&handle);
}
GC.KeepAlive (pixelBufferAttributes);

if (status != CVReturn.Success) {
gchandle.Free ();
Expand Down Expand Up @@ -370,6 +371,7 @@ static unsafe extern CVReturn CVPixelBufferCreateWithPlanarBytes (
}
}
}
GC.KeepAlive (pixelBufferAttributes);

if (status != CVReturn.Success) {
ReleasePlanarBytesCallbackImpl (data_handle_ptr, IntPtr.Zero, 0, 0, IntPtr.Zero);
Expand Down
1 change: 1 addition & 0 deletions src/CoreVideo/CVPixelBufferPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ unsafe extern static CVReturn CVPixelBufferPoolCreatePixelBufferWithAuxAttribute
unsafe {
error = CVPixelBufferPoolCreatePixelBufferWithAuxAttributes (IntPtr.Zero, Handle, allocationSettings.GetHandle (), &pb);
}
GC.KeepAlive (allocationSettings);
if (error != CVReturn.Success)
return null;

Expand Down
9 changes: 9 additions & 0 deletions src/Darwin/SystemLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,17 @@ public IEnumerable<Message> Search (Message msg)
{
if (msg is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (msg));
// The native asl_search call is done here (and not in the iterator method below) to
// keep 'msg' alive across the native call. If it were done in the iterator, the C#
// compiler would hoist 'this' and 'msg' into fields of the generated state machine, and
// the GC.KeepAlive call wouldn't reliably keep 'msg' alive across the native call.
var search = asl_search (Handle, msg.Handle);
GC.KeepAlive (msg);
return EnumerateSearchResults (search);
}

static IEnumerable<Message> EnumerateSearchResults (IntPtr search)
{
IntPtr mh;

while ((mh = aslresponse_next (search)) != IntPtr.Zero)
Expand Down
5 changes: 4 additions & 1 deletion src/Foundation/NSDecimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ public override string ToString ()
{
unsafe {
fixed (NSDecimal* self = &this) {
return new NSString (NSDecimalString (self, NSLocale.CurrentLocale.Handle));
var currentLocale = NSLocale.CurrentLocale;
var rv = new NSString (NSDecimalString (self, currentLocale.Handle));
GC.KeepAlive (currentLocale);
return rv;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Foundation/NSFastEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void Fetch ()
{
unsafe {
count = NSFastEnumerator.objc_msgSend (collection.Handle, Selector.GetHandle ("countByEnumeratingWithState:objects:count:"), state, &state->array1, (nuint) NSFastEnumerationState.ArrayLength);
GC.KeepAlive (collection);
if (!started) {
started = true;
mutationValue = *state->mutationsPtr;
Expand Down
4 changes: 3 additions & 1 deletion src/ImageIO/CGImageDestination.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,9 @@ public bool CopyImageSource (CGImageSource image, CGCopyImageSourceOptions? opti
public void AddAuxiliaryDataInfo (CGImageAuxiliaryDataType auxiliaryImageDataType, CGImageAuxiliaryDataInfo? auxiliaryDataInfo)
{
using (var dict = auxiliaryDataInfo?.Dictionary) {
CGImageDestinationAddAuxiliaryDataInfo (Handle, auxiliaryImageDataType.GetConstant ().GetHandle (), dict.GetHandle ());
var constant = auxiliaryImageDataType.GetConstant ();
CGImageDestinationAddAuxiliaryDataInfo (Handle, constant.GetHandle (), dict.GetHandle ());
GC.KeepAlive (constant);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/ImageIO/CGImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ public CGImageSourceStatus GetStatus (int index)
[SupportedOSPlatform ("maccatalyst")]
public CGImageAuxiliaryDataInfo? CopyAuxiliaryDataInfo (nuint index, CGImageAuxiliaryDataType auxiliaryImageDataType)
{
var ptr = CGImageSourceCopyAuxiliaryDataInfoAtIndex (Handle, index, auxiliaryImageDataType.GetConstant ().GetHandle ());
var constant = auxiliaryImageDataType.GetConstant ();
var ptr = CGImageSourceCopyAuxiliaryDataInfoAtIndex (Handle, index, constant.GetHandle ());
GC.KeepAlive (constant);
if (ptr == IntPtr.Zero)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public partial class NIAlgorithmConvergenceStatusReasonValues {
[UnsupportedOSPlatform ("macos")]
public static NSString GetConvergenceStatusReason (NIAlgorithmConvergenceStatusReason reason)
{
return Runtime.GetNSObject<NSString> (NIAlgorithmConvergenceStatusReasonDescription (reason.GetConstant ().GetHandle ()))!;
var constant = reason.GetConstant ();
var rv = Runtime.GetNSObject<NSString> (NIAlgorithmConvergenceStatusReasonDescription (constant.GetHandle ()))!;
GC.KeepAlive (constant);
return rv;
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/QuickLook/Thumbnail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ public static partial class QLThumbnailImage {

if (scaleFactor != 1 && iconMode != false) {
dictionary = new NSMutableDictionary ();
dictionary.LowlevelSetObject ((NSNumber) scaleFactor, OptionScaleFactorKey.Handle);
dictionary.LowlevelSetObject (iconMode ? CFBoolean.TrueHandle : CFBoolean.FalseHandle, OptionIconModeKey.Handle);
var scaleFactorKey = OptionScaleFactorKey;
dictionary.LowlevelSetObject ((NSNumber) scaleFactor, scaleFactorKey.Handle);
GC.KeepAlive (scaleFactorKey);
var iconModeKey = OptionIconModeKey;
dictionary.LowlevelSetObject (iconMode ? CFBoolean.TrueHandle : CFBoolean.FalseHandle, iconModeKey.Handle);
GC.KeepAlive (iconModeKey);
}

var handle = QLThumbnailImageCreate (IntPtr.Zero, url.Handle, maxThumbnailSize, dictionary.GetHandle ());
Expand Down
Loading
Loading