Instance will be immediately deallocated because property is 'weak'

When compiling my iOS project in Xcode 10 (Swift 4.2), I get the following warning:


Instance will be immediately deallocated because property 'cropConstraint' is 'weak'.


The constraint is declared as:


@IBOutlet weak var cropConstraint: NSLayoutConstraint!


and used as:


cropConstraint = NSLayoutConstraint(
                    item: noCropButton,
                    attribute: .width,
                    relatedBy: .equal,
                    toItem: noCropButton,
                    attribute: .height,
                    multiplier: 1,
                    constant: 0
                )


I checked that my outlet is connected properly.


What am I doing wrong here? Did something change in Xcode 10 that I missed.


Thanks.

Accepted Reply

I think the error message is correct. When you create a new NSLayoutConstraint (line 01 of your second code fragment), that is the only reference to the new constraint object. When you assign it to a weak variable, there is nothing else keeping the object alive (after you exit the scope), so the stored reference will certainly be zeroed.


You don't get this error message "normally" (when the outlet is hooked up in IB and not set in code) because the compiler knows that it doesn't know the lifetime of whatever "cropConstraint" refers to. When you set it in code, the compiler knows that it knows.


Note that for outlets set as a result of nib loading (including outlets in storyboards), there is an object that owns the nib (typically a NSViewController or a NSWindowController), and this object keeps another, strong reference to the outlet's object, keeping it alive. For an exhaustive discussion of this topic, see this document:


https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

Replies

I think the error message is correct. When you create a new NSLayoutConstraint (line 01 of your second code fragment), that is the only reference to the new constraint object. When you assign it to a weak variable, there is nothing else keeping the object alive (after you exit the scope), so the stored reference will certainly be zeroed.


You don't get this error message "normally" (when the outlet is hooked up in IB and not set in code) because the compiler knows that it doesn't know the lifetime of whatever "cropConstraint" refers to. When you set it in code, the compiler knows that it knows.


Note that for outlets set as a result of nib loading (including outlets in storyboards), there is an object that owns the nib (typically a NSViewController or a NSWindowController), and this object keeps another, strong reference to the outlet's object, keeping it alive. For an exhaustive discussion of this topic, see this document:


https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html

You will find some complements in this previous post


https://forums.developer.apple.com/thread/105174