[swift-evolution] A compiler option to warn if a closure captures a strong reference to a class instance?
Robert Widmann
devteam.codafi at gmail.com
Mon Feb 20 12:02:33 CST 2017
It's important to note that strong captures by default are correct and not the issue here. It is reference cycles that are the problem. This feature has been requested before, and SR-1807 has been filed for it.
(For what it's worth, I started working on this a while ago, and a branch tracking this is here: https://github.com/apple/swift/compare/master...CodaFi:hammer-and-cycle).
~Robert Widmann
2017/02/20 6:22、Lauri Lehmijoki via swift-evolution <swift-evolution at swift.org> のメッセージ:
> I'm developing an application where we use RxSwift heavily. RxSwift is a stream library. Consequently, closures that we pass to its combinators often live infinitely (this is because one can use RxSwift to represent infinitely long sequences in time).
>
> Closures with infinite lifespan have implications for the question "what is the best reference capture mode for closures". My experience is that in RxSwift applications, the current default (strong) is almost always suboptimal. It leads to difficult-to-detect memory leaks and introduces a "gotcha" factor to programmers who are new to Swift. I'd prefer the default to be weak capture.
>
> So, I'd like to ask you two things:
>
> A) By default, why the Swift closure captures values strongly?
> B) Should we add a compiler option that, when turned on, would emit a warning if a closure strongly captures a class instance?
>
> Regards
> Lauri
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170220/40a37db9/attachment.html>
More information about the swift-evolution
mailing list