Skip to content
Merged
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
12 changes: 7 additions & 5 deletions src/CoreFoundation/CFBundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ public static CFBundle[] GetAll ()
// '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 just use the size returned by the first
// count to avoid a System.IndexOutOfRangeException when looping
using (var cfBundles = new CFArray (CFBundleGetAllBundles ())) {
var bundleCount = cfBundles.Count;
// 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