Skip to content

[SR-14934] Self can't be assigned weakly within an NSObject's deinit. #3952

Open
@swift-ci

Description

@swift-ci
Previous ID SR-14934
Radar rdar://problem/80820401
Original Reporter hippie_hippo (JIRA User)
Type Bug
Environment

Xcode 13.0 beta 3 (13A5192i)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Foundation
Assignee None
Priority Medium

md5: a4dbd7e0645831e526fb5f08d85de61d

Issue Description:

Self can't be assigned weakly within an NSObject's deinit.

Test case:

import Foundation
protocol FooDelegate: AnyObject {}
class Foo {
  weak var delegate: FooDelegate?
  init(delegate: FooDelegate) {
  self.delegate = delegate
 }
 func cleanup() {}
}
class Bar: NSObject, FooDelegate {
  lazy var foo = Foo(delegate: self)
  deinit {
    self.foo.cleanup()
  }
}
var b: Bar? = Bar()
b = nil

Results in

objc[66436]: Cannot form weak reference to instance (0x600000494070) of class __lldb_expr_25.Bar. It is possible that this object was over-released, or is in the process of deallocation.

Note that this does not occur if self is a plain Swift class.

This is not uncommon in UIViewController code, and can result in hard-to-debug issues.

If this isn't a fixable issue, then I'll post a feature request about better handling of lazy vars in deinit.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions