[swift-evolution] Type based reference cycle detection

Darko Damjanovic darkodamjanovic at me.com
Sun Feb 28 09:02:38 CST 2016

The one thing which I would need most when working with Swift would be type based reference cycle detection during compile time. I have discussed a similar topic already in the developer forums a long time ago and also received some response from one of the Apple Developers. (I think it was Douglas Gregor from this list but I can't find the thread anymore) The answer at this time was that type based reference cycle detection is problematic, but I can not remember the reason anymore. I think the reason was: it is too general and would generate many false positives.

Currently I am working for almost one whole year on a Swift project. (iOS) I have written a few thousands lines of code and of course also experienced some memory leaks. And literally all of them happened in their most basic form:

    class A {
    	let myB: B?
    class B {
       let myA: A?
Which is basically used by the delegate pattern. I understand that cycle detection in a complex graph of objects is not reasonable during compile time. And there are also many cases which can only be found during runtime. I understand also the disadvantages of full runtime-garbage collection. But this is not what I need. The only thing which I miss most is a simple warning of the compiler when type A and B have strong references to each other. (if more is possible - even better)

For example this tool is doing it already: http://j2objc.org/docs/Cycle-Finder-Tool.html

If I am still on the wrong track then I would love to know why. Thanks.

- Darko

More information about the swift-evolution mailing list