# New Crash After Building With Swift 5.5 / Xcode 14

**URL:** <https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798>\
**Category:** Using Swift\
**Created:** [August 22, 2022, 1:16am UTC](https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798 "2022-08-22T01:16:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdkjones](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bdkjones/32/13872_2.png) [@bdkjones](https://forums.swift.org/u/bdkjones)\
**Post date:** [August 22, 2022, 1:16am UTC](https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798/1 "2022-08-22T01:16:26Z")

</div>

# Context

I have an Objective-C method from 2009. Its job is to fetch two types of entities from a Core Data store and then separate those entities into two different arrays (`LPFramework` is a subclass of `LPProject`):

```swift
- (void) populateArraysWithProjects:(NSMutableArray **)projectsArray andFrameworks:(NSMutableArray**)frameworksArray
{
    [*projectsArray removeAllObjects];
    [*frameworksArray removeAllObjects];
    
    NSManagedObjectModel *model = [[self persistentStoreCoordinator] managedObjectModel];
    NSFetchRequest *fetch = [model fetchRequestFromTemplateWithName:@"FetchAllProjectsAndFrameworks" substitutionVariables:@{}];
    NSArray *results = [self.mainMOC executeFetchRequest:fetch error:NULL];
    
    for (LPProject *item in results)
    {
        if ([item isKindOfClass:[LPFramework class]]) {
            [*frameworksArray addObject:item];
        } else {
            [*projectsArray addObject:item];
        }
    }
}

```

# The Problem

In a Swift class, I use this method as follows:

```swift
var projects: NSMutableArray = NSMutableArray(capacity: 50)
var frameworks: NSMutableArray = NSMutableArray(capacity: 50)
        
modelController.populateArrays(withProjects: &projects, andFrameworks: &frameworks)
        
let targetArray: NSMutableArray = (someCondition == true) ? frameworks : projects
        
for item in targetArray {
    // Do stuff
}

```

This has worked for many years. There are no warnings in Xcode. Suddenly, however, when I build the app using Swift 5.5 and Xcode 14, I get an `EXC_BAD_ACCESS` crash when attempting to use `targetArray`. It's as if ARC is now releasing the memory too soon.

I have a few places where I use this method in Objective-C with manual memory management. It continues to work correctly there.

Has something changed in the latest release of Swift that would cause this new behavior? Versions of the app built with older versions of the Swift compiler do not exhibit this crash—even when the `-populateArrays` method is used from Swift code.

---

<div class="post-metadata">

**Author:** ![tclementdev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tclementdev/32/13398_2.png) [@tclementdev](https://forums.swift.org/u/tclementdev)\
**Post date:** [August 22, 2022, 7:08am UTC](https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798/2 "2022-08-22T07:08:26Z")

</div>

Not sure this will help but: you do no need the double pointers `NSMutableArray **`, you can replace it with `NSMutableArray *` and remove the `*` from `*projectsArray` and `*frameworksArray`.

---

<div class="post-metadata">

**Author:** ![bdkjones](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bdkjones/32/13872_2.png) [@bdkjones](https://forums.swift.org/u/bdkjones)\
**Post date:** [August 22, 2022, 8:00am UTC](https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798/3 "2022-08-22T08:00:23Z")

</div>

`NSMutableArray**` and `NSMutableArray*` are two very different things.

The plot thickens: the crash happens only when the Swift compiler is set to `-O (speed)` optimization in Xcode 14 beta 5 (and presumably the earlier Xcode 14 betas as well).

---

<div class="post-metadata">

**Author:** ![tclementdev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tclementdev/32/13398_2.png) [@tclementdev](https://forums.swift.org/u/tclementdev)\
**Post date:** [August 22, 2022, 8:52am UTC](https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798/4 "2022-08-22T08:52:57Z")

</div>

> [@bdkjones](#):
>
> `NSMutableArray**` and `NSMutableArray*` are two very different things.

I'm not sure why you're saying that. What I meant is that you can rewrite your objc code to take `NSMutableArray*` since you are actually not making use of passing `NSMutableArray**`, it might not explain the crash or bug in the compiler but it could maybe provide a way to work around it.

---

<div class="post-metadata">

**Author:** ![conandi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/conandi/32/24861_2.png) [@conandi](https://forums.swift.org/u/conandi)\
**Post date:** [August 24, 2022, 5:59am UTC](https://forums.swift.org/t/new-crash-after-building-with-swift-5-5-xcode-14/59798/5 "2022-08-24T05:59:13Z")

</div>

Since swift prefers value type rather than reference type. It’s possible swift will release heap memory faster. My suggestion is making a local copy before entering iteration loop.
