You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need to communicate to Swift developers how to manage the lifetime of their objects, particularly when handing off weak references.
In a recent Swift 5.4 change, the compiler now destroys objects immediately after their last use instead of keeping them alive until the end of the syntactic scope. In fact, the Swift compiler has always done this in certain cases; it is now doing it much more consistently. In the long run, this consistency actually shields programmers from unexpected behavior. It is also the basis for much more powerful ARC optimization being developed.
The change is only observable in code making invalid assumptions about when weak references are set to nil or when deinitializers run. The fix will generally involve reconsidering your use of `weak` or `unretained` or using `withExtendedLifetime` to explicitly mark the expected lifetime of an object.
There should be some easy-to-understand fairly unobtrusive way to extend the lifetime of a local variable, argument, or 'self' to the end of the lexical scope.
The current best practice for this is:
`defer { withExtendedLifetime(self) {} }`
That looks like quite a hack.
I'm using this radar to gather ideas for alternatives.
For example, we could add a @Keepalive annotation on variable declarations and a @Keepalive on methods that applies to 'self'.
{
@keepaliveletx = X()
container.weakProperty = xcontainer.doStuff()
// x can't be released until the end of scope
}
The text was updated successfully, but these errors were encountered:
Additional Detail from JIRA
md5: 466cc712adde187236a5968500218f90
Issue Description:
We need to communicate to Swift developers how to manage the lifetime of their objects, particularly when handing off weak references.
In a recent Swift 5.4 change, the compiler now destroys objects immediately after their last use instead of keeping them alive until the end of the syntactic scope. In fact, the Swift compiler has always done this in certain cases; it is now doing it much more consistently. In the long run, this consistency actually shields programmers from unexpected behavior. It is also the basis for much more powerful ARC optimization being developed.
The change is only observable in code making invalid assumptions about when weak references are set to nil or when deinitializers run. The fix will generally involve reconsidering your use of `weak` or `unretained` or using `withExtendedLifetime` to explicitly mark the expected lifetime of an object.
There should be some easy-to-understand fairly unobtrusive way to extend the lifetime of a local variable, argument, or 'self' to the end of the lexical scope.
The current best practice for this is:
`defer { withExtendedLifetime(self) {} }`
That looks like quite a hack.
I'm using this radar to gather ideas for alternatives.
For example, we could add a @Keepalive annotation on variable declarations and a @Keepalive on methods that applies to 'self'.
The text was updated successfully, but these errors were encountered: