Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-10119] malloc double free when using SIMD with GCD #622

Closed
swift-ci opened this issue Mar 16, 2019 · 1 comment
Closed

[SR-10119] malloc double free when using SIMD with GCD #622

swift-ci opened this issue Mar 16, 2019 · 1 comment

Comments

@swift-ci
Copy link

@swift-ci swift-ci commented Mar 16, 2019

Previous ID SR-10119
Radar None
Original Reporter damiaan (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Computer ** MacBook Pro (Retina, 15-inch, Late 2013)

OS macOS Mojave 10.14.3 (18D109)

Xcode Version 10.2 beta 4 (10P107d)

Additional Detail from JIRA
Votes 0
Component/s libdispatch, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 10714eca08e38af06d82d157ff8d93f2

Issue Description:

I am stumbling upon an issue with concurrency and SIMD in Swift 5. To reproduce the issue I have simplified my code to the following problem:

  • Initialise an array called `pixels`

  • add async tasks to a concurrent queue

  • in each task: fill a different non overlapping part of the `pixels` array

import Dispatch

let group = DispatchGroup()
let queue = DispatchQueue(
  label: "Concurrent threads",
  qos: .userInitiated,
  attributes: .concurrent
)

let threadCount = 4
let size = 1_000
var pixels = [SIMD3<Float>](
  repeating: .init(repeating: 0),
  count: threadCount*size
)
for thread in 0..<threadCount {
  queue.async(group: group) {
    for number in thread*size ..< (thread+1)*size {
      let floating = Float(number)
      pixels[number] = SIMD3<Float>(floating, floating, floating)
    }
  }
}

print("waiting")
group.wait()
print("Finished")

Executing this using Xcode 10.2 beta 4 in debug mode gives the following error:

Multithread(15936,0x700006447000) malloc: double free for ptr 0x10301b800
Multithread(15936,0x700006447000) malloc: *** set a breakpoint in malloc_error_break to debug
Program ended with exit code: 9

When I build the same program in release mode though, it runs fine and finishes correctly.

@swift-ci
Copy link
Author

@swift-ci swift-ci commented Mar 16, 2019

Comment by Damiaan Dufaux (JIRA)

It turned out I had to use an unsafe mutable buffer pointer like this:

pixels.withUnsafeMutableBufferPointer { pixelsPtr in
    for thread in 0..<threadCount {
        queue.async(group: group) { [pixelsPtr] in
            for number in thread*size ..< (thread+1)*size {
                let floating = Float(number)
                pixelsPtr[number] = SIMD3(floating, floating, floating)
            }
        }
    }
}

Thanks to Rob Napier for clearing this out on stack overflow

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant