<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Jan 30, 2016, at 7:17 AM, Matthias Zenger via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi everyone,<div class=""><br class=""></div><div class="">I'm currently writing a Scheme interpreter in Swift. My original plan was to use Swift's memory management to handle objects in Scheme. Unfortunately, Scheme's data structures are inherently cyclic and I'm struggling to find a good approach to manage memory via ARC.</div><div class=""><br class=""></div><div class="">An example that highlights the problem are arrays that contain elements that refer to the array itself. In Scheme, such arrays can be easily created like this:</div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class=""> (define vec (vector 1 2 3))</font></div><div class=""><font face="monospace, monospace" class=""> (vector-set! vec 1 vec)</font></div><div class=""><br class=""></div><div class="">Vector <font face="monospace, monospace" class="">vec</font> now refers to itself via index 1.</div><div class=""><br class=""></div><div class="">For representing a Scheme array, my interpreter simply uses an object that wraps a Swift array. The array elements point at Scheme objects. So, they could point at the array itself. Since I don't want to loose objects that are linked off the array only, I need to use strong references in the array. Obviously, this creates a serious memory leak. Here's some code illustrating the design and the construction of <font face="monospace, monospace" class="">vec</font>:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class=""> enum SchemeValue {</font></div><div class=""><font face="monospace, monospace" class=""> case Nil</font></div><div class=""><font face="monospace, monospace" class=""> case Num(Int)</font></div><div class=""><font face="monospace, monospace" class=""> indirect case Cons(SchemeValue, SchemeValue)</font></div><div class=""><font face="monospace, monospace" class=""> case Vector(WrappedArray<SchemeValue>)</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""> class WrappedArray<T> {</font></div><div class=""><font face="monospace, monospace" class=""> var array: [T]</font></div><div class=""><font face="monospace, monospace" class=""> init(_ elem: T...) {</font></div><div class=""><font face="monospace, monospace" class=""> self.array = elem</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""> }</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class=""> let wa = WrappedArray<SchemeValue>(.Num(1), .Num(2), .Num(3))</font></div><div class=""><font face="monospace, monospace" class=""> let vec = SchemeValue.Vector(wa)</font></div><div class=""><font face="monospace, monospace" class=""> wa.array[1] = vec</font></div></div><div class=""><br class=""></div><div class="">I haven't found a good way to address the issue. What I would ideally need is manual access to retain/release functions. I would also be willing to write my own memory manager for such objects, but also this seems to be impossible in Swift. As a workaround, I currently built a mark/sweep garbage collector for all Scheme objects I allocate (by keeping them all in a weak object pool — also hand-written because it doesn't exist in Swift). But I consider this application-specific garbage collector that runs on top of ARC a huge heck.</div><div class=""><br class=""></div><div class="">So, my question is: What are Swift programmers supposed to do if they are dealing with inherently cyclic data structures that cannot be broken up via weak references? It almost seems like there's some memory management functionality missing in Swift that makes this possible.</div></div></div></blockquote><br class=""></div><div>Doing your own GC is probably the best you can do today. You can manually retain and release references to Swift classes using the Unmanaged<T> type, which might help interoperate Swift ARC objects with your GC.</div><div><br class=""></div><div>-Joe</div><div><br class=""></div></body></html>