SwiftData inside Actor leads to memory leaks

i am developing a SwiftUI App, where i need to work with relatively large amounts of data sets. While processing these data i had some issues with my app crashing randomly. As i was debugging this situation for a while i found out that dataraces were the cause for these crashes. That is why i decided to use an actor for these things.. As the actor takes care of concurrent threads, i was not having any crashes anymore, BUT, now i have to deal with some memory leaks! i've created a simple demo project to reproduce these leaks.

my view:

struct ContentView: View {
    @Environment(\.modelContext) private var modelContext
//    @Query private var items: [Item]
    

    var body: some View {
        VStack {
            Button(action: {
                Task { await testImport() }
            }, label: {
                Text("Import")
            })
        }
    }
}

the function:

 func testImport() async {
        let actorX = testActor(container: self.modelContext.container)
        await actorX.cleanUp()

        // create dummy data:
        var dummyArray: [Int] = []
        
        for i in 0...1300 {
            dummyArray.append(i)
        }

        await actorX.saveAssets(with: dummyArray)
        dummyArray = []
        print("Checkpoint")
    }

the actor:

actor testActor {
    
    public var modelContainer: ModelContainer
    public var modelExecutor: any ModelExecutor
    private var context: ModelContext { modelExecutor.modelContext }

    public init(container: ModelContainer) {
        self.modelContainer = container
        let context = ModelContext(modelContainer)
        modelExecutor = DefaultSerialModelExecutor(modelContext: context)
    }

    
    func cleanUp() {
        print("starting cleanup...")
        do {
            try context.delete(model: Item.self)
            print("cleanup: table LocalAsset truncated")
        } catch {
            print("Failed to clear all LocalAsset data.")
        }
    }
    
    func saveAssets(with array: [Int]) {

        for i in 0..<array.count {
            let foo = array[i]
            let newItem = Item(timestamp: Date(), dummyInt: foo)

            context.insert(newItem)
        }
        try? context.save()
    }
}

And Here's a screenshot of Xcode's Instruments Leak tool:

i hope somebody has any idea how to get rid of those leaks..

while debugging this for some time already, i observed that the leaks happen during the init of the actor, and even without any actor at all, as soon as i try to create a separate modelContext! seems like SwiftData cannot deal with two separate modelContext without leaking memory.

Hi, I'm using ModelActor to avoid doing query operations in the main thread, so I'm doing them in concurrent threads. However, I'm having the same problem. I'm having a memory leak that I can't figure out why. Have you been able to solve the problem? Will it cause a problem when I use it like this? I'm thinking of uploading the app to the app store, will Apple cause a problem? Thank you very much. I would be very grateful if you share your experience.

@FiratSirin no, besides the fact that those leaks are just visible inside the Xcode instruments, my app works just fine. those leaks do not seem to cause any problems (yet) i talked to apple about this issue during a wwdc24 labs session, and apple told me to open a bug report for this.. It should be fixed with iOS18. So i created a bug report with ID: FB13844786 for that topic

You should find this fixed in the iOS 18 beta 1.

SwiftData inside Actor leads to memory leaks
 
 
Q