Memory Leak in Swift Structures

Before WWDC I encountered a weird behavior in Swift 1.2 where structures caused memory leaks. I posted a question about this issue on Stack Overflow: http://stackoverflow.com/questions/30628547/memory-leak-in-swift-structures-how-to-fix-this


Now I tested this problem with Swift 2.0 and sadly it's not fully resolved. Since the old dev forums are now obsolete as far as I understand it, I'll try to explain the issue once again here.


I'm developing an application in Swift and I'm trying to use value types (structs and enums) where possible. According to the documentation about memory management, working with value types should not cause any memory problems and it should just work. But I found a situation where memory is leaking when using structs. And because it's in my event handling code, the leaks are pretty big.


Let's say there is a protocol

Item
which defines a single property
value
:


protocol Item {
    var value: String { get }
}


We then create a concrete struct which implements the

Item
protocol and adds an additional generic property
additionalValue
of type T. Let's call the struct
FooItem
.


struct FooItem<T>: Item {

    let value: String
    let additionalValue: T

    init(value: String, additionalValue: T) {
        self.value = value
        self.additionalValue = additionalValue
    }

}


The third piece of the puzzle is another struct which wraps an item implementing the

Item
protocol. It's called
ItemWrapper
.


struct ItemWrapper {

    let item: Item

    init(item: Item) {
        self.item = item
    }

}


Every time an ItemWrapper value is created with a FooItem, there is a memory leak shown in Instruments.


let item = FooItem(value: "protocol value", additionalValue: "foo item value")
let _ = ItemWrapper(item: item)


You can look for some Intruments screenshot in the original question posted on Stack Overflow: http://stackoverflow.com/questions/30628547/memory-leak-in-swift-structures-how-to-fix-this


There is also a Gist with the whole code: https://gist.github.com/lukaskubanek/4e3f7657864103d79e3a


I'd like to ask whether this is a bug or whether I am doing anything wrong? Thanks for any help with this issue!

Answered by lukaskubanek in 39704022

The issue was resolved in Xcode 7 beta 5: http://stackoverflow.com/a/31936407/670119

I'm using similar techniques and large numbers of structures, but haven't looked to see if I have a leak too. Now I'm worried...


In any case, you didn't show a bug report number (rdar) - did you enter one? If not this should have been your first action (bugreporter.apple.com).


Structures are value types. If they only contain other value types, how could you possibly get a user-caused memory leak when instantiating then releasing them?


My suggestion to push this forward would be to craft the a demo project demonstrating the problem (for instance, a single view iOS app with a button that creates 10,000 objects and then frees them, with the result of a huge increase in app memory usage each time the button is pressed. [It might rise the first time due to loading a library, etc.] Then, once you can show the leak, go back to the code and try simplifying it so that in the end you have a single issue that with it you get the leak, then without it you don't. If you get that far, then come back here, post a new question - "Why does use of blah blah cause a memory leak, and is there a workaround?" with the details on that one issue that causes the leak to occur (and you bug number). You'll find you will get more attention than a general "my huge chunk of code leaks".


Good luck!

Hi, the posted code actually is a minimal example. You can download the Xcode project from here: https://www.dropbox.com/s/z6ugxzxqggrv1xl/SwiftStructsMemoryLeak.zip?dl=0


I already reported a bug (21375421). It's really just a summary of this post with a step-by-step description for running the attached Xcode project.

Accepted Answer

The issue was resolved in Xcode 7 beta 5: http://stackoverflow.com/a/31936407/670119

Is this an actual memory leak, or a false positive from Instruments?


Important for people running 1.2 to know if they'll need to upgrade to 2.0 to avoid this leak.

I haven't find out whether it was a memory leak or just a bug in Instruments. Since I adopted Swift 2.0 already in the early beta versions, I haven't look further into this issue.

Memory Leak in Swift Structures
 
 
Q