Xcode 7.0 (7A120f): [Swift] Swift functions accepting an inout Array parameter have memory management issues when stored in an array

Originator:janoschhildebrand
Number:rdar://21329377 Date Originated:11-Jun-2015
Status:Closed Resolved:Xcode 7.1b2
Product:Developer Tools Product Version:Xcode 7.0 (7A120f)
Classification:Serious Bug Reproducible:Always
 
Summary:
The following example crashes reliably on my machine:

// Function - fine by itself
func notFun(inout target: Array<Int>) {}
// Will wreak havoc when stored in array
let funs = [notFun]

// An innocent array
var target = Array<Int>(count: 42, repeatedValue: 42)

// Run the function
funs.first?(&target)

// Do some more work to trigger crash in this case
print(target)


The general cause seem to be functions with an inout parameter of type Array that are stored in an array themselves. The array's backing store seems to be mismanaged in some way in that case.

Depending on the actual code, I have seen this pattern work fine in rare cases or mostly cause crashes by:
* Your average EXC_BAD_ACCESS
* malloc aborting when it detects some corrupted data structures
* fatal error: UnsafeMutablePointer.moveInitializeFrom non-following overlapping range; use moveInitializeBackwardFrom

Address sanitizer in some cases detects writes to freed memory, in other cases the code works fine with address sanitizer enabled.


An example project is attached and I can provide more examples if that would be of use.

Steps to Reproduce:
Run the attached Xcode project

Expected Results:
Functions with inout Array<T> parameters that are stored in arrays should have correct memory management

Actual Results:
They don't ;-)

Version:
Xcode 7.0 (7A120f)
Apple Swift version 2.0 (swiftlang-700.0.38.1 clang-700.0.53)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!