<div style="font-family:UICTFontTextStyleBody;font-size:17px">Swift won't mess with memory it does'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'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'm using for sockets right now:</div><div style="font-family:UICTFontTextStyleBody;font-size:17px"><a href="https://github.com/mike-ferenduros/SwiftySockets">https://github.com/mike-ferenduros/SwiftySockets</a></div><div><br></div><br>On Tuesday, October 4, 2016, Etan Kissling via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> 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/<wbr>manpages/xenial/en/man3/<wbr>getaddrinfo.3.html</a><br>
<br>
To simplify the API, I'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 -> [addrinfo] {<br>
var err: Int32<br>
var res: UnsafeMutablePointer<addrinfo><wbr>?<br>
if var hints = hints {<br>
err = getaddrinfo(node, service, &hints, &res)<br>
} else {<br>
err = getaddrinfo(node, service, nil, &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'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'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 href="javascript:;" onclick="_e(event, 'cvml', 'swift-users@swift.org')">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
</blockquote>