I am using Swift for several months now, and really like the language. While Xcode support is not so great, the language significantly improves my daily coding sessions. However, there are aspects of Swift that are not really clear to me. One of them is objects initialization issue which quietly causing memory leaks.

What's wrong with init?

Let's assume that we have MyObject class, which objects does nothing except logging to the console whenever they are initialized or de-initialized:

class MyObject {
    init() {
        NSLog("MyObject init")
    deinit {
        NSLog("MyObject deinit")

We also have custom view controller, that extends UIViewController, and holds MyObject instance using constant reference (let):

class ViewController: UIViewController {
    let object = MyObject()

Nothing complicated so far, everything is clear. So, imagine you want to initialize a ViewController object:

let myViewController = ViewController()

Looks good? Well, it's not. You just created a memory leak, which can be confirmed by profiling the app using Xcode's Instruments. To better understand what is going on, you can change your code, so ViewController object will be initialized and immediately deallocated:

var myViewController: ViewController? = ViewController()
myViewController = nil

Now, check out the console output:

MyObject init
MyObject init
MyObject deinit

Yes, that's right. Calling ViewController() is causing MyObject instance to be initialized twice, in fact there are two instances of MyObject. One of them is hold by object property in ViewController, another one - just leaked and probably will never be deallocated.

How is that even possible?

My guess is that UIViewController init method could be implemented like this:

- (id)init {
    self = [super init];
    if (self) {
        self = [[self.class alloc] initWithNibName:nil bundle:nil];
    return self;

While this should work in Objective-C world, it's problematic in Swift. There is no such thing like automatic property initialization in Objective-C, or default property values. However, in Swift properties are initialized before the object that defines them is initialized, automatically.

That's only my guess, we can't be sure if that's really a reason of the issue. For sure, every Swift developer should pay extra attention when initializing Foundation, UIKit, AppKit or other pre-Swift frameworks. I can confirm that this issue can be reproduced not only with UIViewController, but also UIButton and many other UIKit classes.

Avoiding such nonsense

Thankfully, there is very easy way no avoid such memory leaks. You should always choose the correct initializer for given class.

You can call init without parameters on UIViewController because (like many others) it extends from NSObject. But when you look inside documentation, you will see that the UIViewController initializer is initWithNibName:bundle:.

It's a pity Swift can't warn us when we are trying to init UIViewController with init(), as it looks obviously wrong and you should never do that. Perhaps it will be improved in future releases.

Tested under Xcode 6.1.1 and Xcode 6.2 beta

  1. hookup apps says:

    Hookup Women Uses Free of charge Matters? An Incredible Side to side Benefit!

    Cost-free hookup ladies hookup apps online is the best solution if you’re tired with gonna bars and night
    clubs merely to be prevented, and even even worse, laughed at.
    I am aware what it’s like because I’ve been there.
    I used to be one and eager during the working day — I necessary a fresh lover —
    but I continued trying because I needed no other choice.

    If you’re an individual gentleman who would like to hookup with sexy
    ladies without gonna those areas in which the girls are alone, then this report might just make positive changes to life.
    It can describe why internet dating on the web is the greatest choice
    if you’re a masculine who may be shy to approach
    a beautiful woman in a pub or membership.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>