Hi Apple Developer Community,
I'm facing a crash when updating an array of tuples from both a background thread and the main thread simultaneously. Here's a simplified version of the code in a macOS app using AppKit:
class ViewController: NSViewController {
var mainthreadButton = NSButton(title: "test", target: self, action: nil)
var numbers = Array(repeating: (dim: Int, key: String)(0, "default"), count: 1000)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(mainthreadButton)
mainthreadButton.translatesAutoresizingMaskIntoConstraints = false
mainthreadButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
mainthreadButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
mainthreadButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
mainthreadButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
mainthreadButton.target = self
mainthreadButton.action = #selector(arraytest(_:))
}
@objc func arraytest(_ sender: NSButton) {
print("array update started")
// Background update
DispatchQueue.global().async {
for i in 0..<1000 {
self.numbers[i].dim = i
}
}
// Main thread update
var sum = 0
for i in 0..<1000 {
numbers[i].dim = i + 1
sum += numbers[i].dim
print("test \(sum)")
}
mainthreadButton.title = "test = \(sum)"
}
}
This results in a crash with the following message:
malloc: double free for ptr 0x136040c00
malloc: *** set a breakpoint in malloc_error_break to debug
What's interesting:
This crash only happens when the tuple contains a String ((dim: Int, key: String))
If I change the tuple type to use two Int values ((dim: Int, key: Int)), the crash does not occur
My Questions:
Why does mutating an array of tuples containing a String crash when accessed from multiple threads?
Why is the crash avoided when the tuple contains only primitive types like Int?
Is there an underlying memory management issue with value types containing reference types like String?
Any explanation about this behavior and best practices for thread-safe mutation of such arrays would be much appreciated.
Thanks in advance!
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi all,
I’m looking for a way to automate the process of adding Swift Package dependencies (SPM) into an Xcode project.
Right now, I’m doing this manually via:
Xcode → Project → Swift Packages → Add Package Dependency...
But I’d like to script this step — ideally as part of a CI/CD pipeline or a setup script. I explored the xcodeproj Ruby gem, but it doesn’t support adding SPM packages, and there's no official support for editing the .xcodeproj structure related to Swift Packages.
So my question is:
Is there any Apple-supported or community-supported way to programmatically add Swift Package dependencies to an Xcode project or workspace?
Would love to know if anyone has:
Used xcodebuild, xcodegen, or other tools for this
Explored editing project files directly (like project.pbxproj or Package.resolved)
Found any API or CLI hooks from Apple for this
Thanks in advance!
Topic:
Developer Tools & Services
SubTopic:
Xcode