Ach, apologies - I forgot that addrinfo contains pointers. Ignore what I said :)<br><br>On Tuesday, October 4, 2016, Mike Ferenduros &lt;<a href="mailto:mike.ferenduros@gmail.com">mike.ferenduros@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="font-family:UICTFontTextStyleBody;font-size:17px">Swift won&#39;t mess with memory it does&#39;t own, and memory the addrinfo list falls into that category.</div><div style="font-family:UICTFontTextStyleBody;font-size:17px"><br></div><div style="font-family:UICTFontTextStyleBody;font-size:17px">The addrinfo you&#39;re accessing through pointee  is a struct which means that when you assign it somewhere you get a *copy* of the thing pointed to. Swift owns that copy and will manage it properly, and leave the original alone.</div><div style="font-family:UICTFontTextStyleBody;font-size:17px"><br></div><div style="font-family:UICTFontTextStyleBody;font-size:17px">Fwiw, this is the wrapper I&#39;m using for sockets right now:</div><div style="font-family:UICTFontTextStyleBody;font-size:17px"><a href="https://github.com/mike-ferenduros/SwiftySockets" target="_blank">https://github.com/mike-<wbr>ferenduros/SwiftySockets</a></div><div><br></div><br>On Tuesday, October 4, 2016, Etan Kissling via swift-users &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-users@swift.org&#39;);" target="_blank">swift-users@swift.org</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">POSIX getaddrinfo allocates memory that must later be freed using freeaddrinfo.<br>
See <a href="http://manpages.ubuntu.com/manpages/xenial/en/man3/getaddrinfo.3.html" target="_blank">http://manpages.ubuntu.com/man<wbr>pages/xenial/en/man3/getaddrin<wbr>fo.3.html</a><br>
<br>
To simplify the API, I&#39;ve created this function:<br>
<br>
        import Foundation<br>
<br>
        enum SystemError: Swift.Error {<br>
            case getaddrinfo(Int32, Int32?)<br>
        }<br>
<br>
        public func getaddrinfo(node: String?, service: String?, hints: addrinfo?) throws -&gt; [addrinfo] {<br>
            var err: Int32<br>
            var res: UnsafeMutablePointer&lt;addrinfo&gt;<wbr>?<br>
            if var hints = hints {<br>
                err = getaddrinfo(node, service, &amp;hints, &amp;res)<br>
            } else {<br>
                err = getaddrinfo(node, service, nil, &amp;res)<br>
            }<br>
            if err == EAI_SYSTEM {<br>
                throw SystemError.getaddrinfo(err, errno)<br>
            }<br>
            if err != 0 {<br>
                throw SystemError.getaddrinfo(err, nil)<br>
            }<br>
            defer {<br>
                freeaddrinfo(res)<br>
            }<br>
            var result = [addrinfo]()<br>
            var ai = res?.pointee<br>
            while ai != nil {<br>
                result.append(ai!)<br>
                ai = ai!.ai_next?.pointee<br>
            }<br>
            return result<br>
        }<br>
<br>
I don&#39;t feel that the function is correct, though.<br>
<br>
• How can the Swift memory model know that getaddrinfo allocates memory, and that Swift should not overwrite that memory with own stuff?<br>
• How can Swift know that freeaddrinfo deletes the whole list, and that it should copy out ai information that has been assigned to the result array?<br>
<br>
What&#39;s the correct way to interface with getaddrinfo?<br>
<br>
Thanks<br>
<br>
Etan<br>
<br>
<br>
______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a>swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
</blockquote>
</blockquote>