Kate Belinskaya
Kate Belinskaya

Reputation: 66

Memory Leak of Dictionary used in singleton

I have singleton. It contains a 2 dictionaries.

struct Stat {
    var statHash:String
    var displayDescription:String
    var displayName:String
    var displayIcon:String
    var statIdentifier:String
}

class Singleton {

    static let sharedInstance = Singleton()

    var statsDesc = [String:Stat]()
    var test = [String: String]()

    init() {
        test["a"] = "b"
    }
}

let singlton = Singleton.sharedInstance

On using the leaks tool, I am getting a memory leak of the second dictionary(String, String).

enter image description here

Could someone please explain why this happens?

Link to the project on dropbox

Thanks for the help.

Upvotes: 0

Views: 596

Answers (2)

Kate Belinskaya
Kate Belinskaya

Reputation: 66

It was swift bug Memory Leak from Multiple Dictionary Properties

Upvotes: 1

Paulw11
Paulw11

Reputation: 114975

In effect, a singleton is a leak. Since the singleton holds a reference to itself, it is never released and neither are any of its properties.

The Leaks tool noted that singlton went out of scope, but the memory that was allocated was not released, so it flags a leak. In this case, however, a leak is exactly what you want.

Upvotes: 0

Related Questions