Memory Leak in Generic Structures in Swift 2.0

Originator:kubanek.l
Number:rdar://21375421 Date Originated:14.06.2015
Status:Closed Resolved:yes
Product: Product Version:
Classification: Reproducible:
 
Summary:
When a struct (FooItem<T>) which conforms to a custom protocol (Item) has a generic property (additionalValue) is contained within another struct (ItemWrapper) which references the protocol, a memory is leaked every time the outer struct is initialized.

Steps to Reproduce:
1. Open the attached Xcode project SwiftStructsMemoryLeak.xcodeproj in Xcode 7.
2. Select ‘Profile’ in the toolbar or press CMD+I.
3. Select the Leaks configuration in Instruments.
4. Press the record button.
5. Let the application run for a few seconds.
6. Check out the list of memory leaks by selecting the Leaks instrument in the timeline.

Expected Results:
There should not be any memory leak listed.

Actual Results:
There is a leaked object described as 48 Bytes of allocated memory which is created in the libswiftCore.dylib in swift_slowAlloc.

Regression:
Originally, I had a problem with memory leaks in structs already in Swift 1.2 and posted a question about it on Stack Overflow: http://stackoverflow.com/questions/30628547/memory-leak-in-swift-structures-how-to-fix-this. The problem didn’t include the generic property and caused memory leaks when accessing the inner struct using convenience properties on the outer struct. This issue was resolved in Swift 2.0.

Notes:
I haven’t found a workaround for this problem. I guess I would have to remove the approach involving generics which I think is pretty beneficial.

Here is my unapproved question on the new dev forums: https://forums.developer.apple.com/message/9643#9643

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!