9

I found a memory leak in Swift. It gave me nightmares because my code was full of small leaks everywhere, and then I managed to reduce it to this small example,

import UIKit

enum LeakingEnum {
    case
    LeakCase,
    AnotherLeakCase
}

class Primitive {
    var lightingType: LeakingEnum = .LeakCase
    var mysub : [Int] = []
    init() {
        mysub.append(80)
    }
}

class ViewController: UIViewController {
    var prim: Primitive?
    override func viewDidLoad() {
        super.viewDidLoad()
        prim = Primitive()
    }
}

If you run this program on an iPhone and Profile with Instruments, you'll find this leak in Array._copyToNewBuffer,

Memory leak detected with Instruments

If I remove the call to mysub.append, it stops leaking. If I remove the enum from Primitive, it stops leaking as well. All the classes where I have an enum leak like this. What's going on with Swift enums?

Reproduced in Swift 3, Xcode 8.2.1, and iOS 10.2, on both an iPhone6 and an iPad Pro. Can't reproduce in the Simulator, or in a device with iOS 9.3.2. You can download a minimal sample app here: https://github.com/endavid/SwiftLeaks

Is this a known bug? Is there any work around?

Edit:

Because this remind me of another enum bug, Accessor gives the wrong value in Swift 1.2/2.0 Release build only, I tried making the enum an @objc Int enum, but it still leaks. However, making lightingType directly an Int does fix the leak...

Edit2: After updating my iPhone to 10.3 and Xcode to 8.3, the leak is gone. It seems it was an issue of iOS 10.2...

7
  • 4
    Since you've done an excellent job distilling this to a small example, the best place to pursue this is by opening a defect at bugs.swift.org.
    – Rob Napier
    Mar 4, 2017 at 23:00
  • Puzzling. By definition, a leak is a memory allocation that remains after the object is no longer in use. I moved the var Prim into the viewDidLoad function, and referenced it to make sure the compiler didn't remove it. No leak! Perhaps an instrumentation problem?
    – Bill
    Mar 4, 2017 at 23:18
  • Is this a Swift problem, or something with the implementation? I don't have Linux to try other implementations... So I reported it to Apple (bug number 30856358)
    – endavid
    Mar 5, 2017 at 10:11
  • I created the same test case as a Cocoa (Mac) app. It does not leak. Also your test case does not report a leak under the simulator. So definitely an Apple issue.
    – Bill
    Mar 7, 2017 at 23:35
  • Thanks for checking. Apple replied saying they can't reproduce the problem... I also tried with the Simulator and it doesn't leak... It does leak on my iPhone 6 with 10.2, and I updated to 10.2.1 today and it still leaks.
    – endavid
    Mar 8, 2017 at 23:45

1 Answer 1

4

Hey @endavid managed to replicate the issue consistently. We spend a good time trying to figure out what was going on and your post helped a lot!

Here is the sample repo: https://github.com/Giphy/ios-memory-leak-sample

Radar: https://openradar.appspot.com/radar?id=4992108083544064

We are developing SDKs and same exact issue surfaced with a small difference. Since we wanted things to interop we added @objc to the enum definition and things started to leak exactly the way you described given your class has two properties, one enum and one mutable array.

Consistently reproduced the leak:

// Without @objc this enum won't leak
// however when this enum is included in a class
// which contains an array, it will leak
@objc enum leakingObjCMarkedEnum: Int {

    // Just some random cases.
    case apple, orange
}

// Wrapper class which contains an enum and Array
// The class needs to contain the the Array in order for
// the Enum to leak.
class WrapperClass {

  // Optional enums marked with @objc will leak.
  var leakyOptionalEnum: leakingObjCMarkedEnum?

  // Include an array to trigger this behaviour.
  // Empty arrays won't cause the leak, so lets add an arbitrary Int
  var myArray: [Int] = [80]
}

class ViewController: UIViewController {

  // Hang on to a reference to our Wrapper Class instance.
  var wc: WrapperClass?

  override func viewDidLoad() {
    super.viewDidLoad()

    // Allocate an instance of our class
    // and things will start leaking at this point.
    wc = WrapperClass()
  }
}

Work Around:

If we convert the optional enum class property to a non-optional, leak will disappear.

// Let's convert the optional property to a non-optional
var leakyOptionalEnum: leakingObjCMarkedEnum = .orange

Edit:

It is fixed by guys @ Apple: https://bugs.swift.org/browse/SR-5625 PR: https://github.com/apple/swift/pull/11341

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.