Swift interpolated strings leaking memory

Originator:sam
Number:rdar://26366199 Date Originated:19-May-2016 07:45 PM
Status:Closed Resolved:
Product:Developer Tools Product Version:
Classification:Performance Reproducible:Always
 
Summary:
Using interpolated strings in Swift can cause memory leaks to be reported in Instruments. Changing the code from interpolating to concatenating the strings fixes the leak.

I’ve attempted to shrink the repro case down as much as I can, from what I can gather, the problem only occurs when a largish value type is assigned within the same scope. There are other factors involved that I couldn’t discern - making minor changes to the size/structure of the value types can result in the leak disappearing, but the structure I’ve supplied in the sample code will reproduce the issue every time. 

Note the types need to be declared in a separate file, and the code compiled -O (fast) with whole module optimisations off.

The relevant code is also available at https://gist.github.com/samritchie/81172456003141415f2fddcd4d3e50e9 if there’s an issue with the zip.

Steps to Reproduce:
1. Download the attached project zip and unzip
2. Open in Xcode and select a simulator or device
3. Profile the app in Instruments, selecting the 'Leaks' instrument if prompted (Simulator or Device is okay)
4. Comment out the string interpolation at AppDelegate.swift#20 and uncomment the concatenation at #23
5. Profile the app again


Expected Results:
There shouldn’t be any memory leaks, regardless of whether interpolation or concatenation is used

Actual Results:
Instruments detects a memory leak in the interpolation case

Version:
Xcode Version 7.3.1 (7D1014)
OS X 10.11.4 (15E65)
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)

Notes:


Configuration:
Apple Swift v 2.2 (Xcode 7.3.1). I’ve only tested iOS/iOS Simulator and this specific version of Swift.

Comments

It wasn't fixed yet in Xcode 10/Swift 4.2

Find leaks in _StringGuts.withMutableStorage. This is any concatenation of String, or String's UnicodeScalarView. I see this issue in own project when variable string has append another part, and PMJSON project when parse and concatenate characters to UnicodeScalarView of String

`+0xb6 movq %rax, %rdi +0xb9 testq %rdx, %rdx +0xbc jne "specialized _StringGuts.withMutableStorage<A, B>(of:unusedCapacity:_:)+0xc5" << IN THIS FUNCTION WE CAN SEE LEAK +0xbe movq %rdi, 2056467(%rip) +0xc5 leaq 32(%r14,%r14), %rsi +0xca movl $7, %edx +0xcf callq "swift_allocObject" << LEAK HERE +0xd4 movq %rax, %r14 +0xd7 movq %r14, %rdi +0xda callq "_stdlib_malloc_size" `

By subach.pavel at Jan. 18, 2019, 9:28 a.m. (reply...)

Appears to be fixed in Xcode 8/Swift 3


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!