Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/CoreFoundation/CFArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
using ObjCRuntime;

using CFIndex = System.nint;
using CFArrayRef = System.IntPtr;
using CFAllocatorRef = System.IntPtr;

namespace CoreFoundation {

Expand Down Expand Up @@ -149,6 +151,11 @@ public static nint GetCount (IntPtr array)
{
return CFArrayGetCount (array);
}

[DllImport (Constants.CoreFoundationLibrary)]
extern static CFArrayRef CFArrayCreateCopy (CFAllocatorRef allocator, CFArrayRef theArray);

public CFArray Clone () => new CFArray (CFArrayCreateCopy (IntPtr.Zero, this.Handle), true);
}
}

19 changes: 15 additions & 4 deletions src/CoreFoundation/CFBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,22 @@ public static CFBundle[] GetBundlesFromDirectory (NSUrl directoryUrl, string bun

public static CFBundle[] GetAll ()
{
using (var cfBundles = new CFArray (CFBundleGetAllBundles ())) {
var managedBundles = new CFBundle [cfBundles.Count];
for (int index = 0; index < cfBundles.Count; index++) {
// as per apple documentation:
// CFBundleGetAllBundles
//
// 'This function is potentially expensive and not thread-safe'
//
// This means, that we should not trust the size of the array, since is a get and
// might be modified by a diff thread. We are going to clone the array and make sure
// that Apple does not modify the array while we work with it. That avoids changes
// in the index or in the bundles returned.
using (var cfBundles = new CFArray (CFBundleGetAllBundles ()))
using (var cfBundlesCopy = cfBundles.Clone () ) {
var bundleCount = cfBundlesCopy.Count; // the property is a C call, calling everytime we loop is not needed
var managedBundles = new CFBundle [bundleCount];
for (int index = 0; index < bundleCount; index++) {
// follow the get rule, we do not own the object
managedBundles [index] = new CFBundle (cfBundles.GetValue (index), false);
managedBundles [index] = new CFBundle (cfBundlesCopy.GetValue (index), false);
}
return managedBundles;
}
Expand Down
1 change: 0 additions & 1 deletion tests/xtro-sharpie/common-CoreFoundation.ignore
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@
!missing-pinvoke! CFArrayApplyFunction is not bound
!missing-pinvoke! CFArrayBSearchValues is not bound
!missing-pinvoke! CFArrayContainsValue is not bound
!missing-pinvoke! CFArrayCreateCopy is not bound
!missing-pinvoke! CFArrayCreateMutable is not bound
!missing-pinvoke! CFArrayCreateMutableCopy is not bound
!missing-pinvoke! CFArrayExchangeValuesAtIndices is not bound
Expand Down