<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 class="">Yup (BTW I'm fine with the "ai_next" item becoming useless in the copies. I just want to copy out the other parts in a way that gets copied reliably)</div>
<br class="">
<div>
<blockquote type="cite" class="">
<div class="">On 4 Oct 2016, at 19:26, Mike Ferenduros 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="">Ach, apologies - I forgot that addrinfo contains pointers. Ignore what I said :)<br class="">
<br class="">
On Tuesday, October 4, 2016, Mike Ferenduros <<a href="mailto:mike.ferenduros@gmail.com" class="">mike.ferenduros@gmail.com</a>> wrote:<br class="">
<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" class="">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" class=""><br class="">
</div>
<div style="font-family:UICTFontTextStyleBody;font-size:17px" class="">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" class=""><br class="">
</div>
<div style="font-family:UICTFontTextStyleBody;font-size:17px" class="">Fwiw, this is the wrapper I'm using for sockets right now:</div>
<div style="font-family:UICTFontTextStyleBody;font-size:17px" class=""><a href="https://github.com/mike-ferenduros/SwiftySockets" target="_blank" class="">https://github.com/mike-<wbr class="">ferenduros/SwiftySockets</a></div>
<div class=""><br class="">
</div>
<br class="">
On Tuesday, October 4, 2016, Etan Kissling via swift-users <<a href="javascript:_e(%7B%7D,'cvml','swift-users@swift.org');" target="_blank" class="">swift-users@swift.org</a>> wrote:<br class="">
<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 class="">
See <a href="http://manpages.ubuntu.com/manpages/xenial/en/man3/getaddrinfo.3.html" target="_blank" class="">
http://manpages.ubuntu.com/man<wbr class="">pages/xenial/en/man3/getaddrin<wbr class="">fo.3.html</a><br class="">
<br class="">
To simplify the API, I've created this function:<br class="">
<br class="">
import Foundation<br class="">
<br class="">
enum SystemError: Swift.Error {<br class="">
case getaddrinfo(Int32, Int32?)<br class="">
}<br class="">
<br class="">
public func getaddrinfo(node: String?, service: String?, hints: addrinfo?) throws -> [addrinfo] {<br class="">
var err: Int32<br class="">
var res: UnsafeMutablePointer<addrinfo><wbr class="">?<br class="">
if var hints = hints {<br class="">
err = getaddrinfo(node, service, &hints, &res)<br class="">
} else {<br class="">
err = getaddrinfo(node, service, nil, &res)<br class="">
}<br class="">
if err == EAI_SYSTEM {<br class="">
throw SystemError.getaddrinfo(err, errno)<br class="">
}<br class="">
if err != 0 {<br class="">
throw SystemError.getaddrinfo(err, nil)<br class="">
}<br class="">
defer {<br class="">
freeaddrinfo(res)<br class="">
}<br class="">
var result = [addrinfo]()<br class="">
var ai = res?.pointee<br class="">
while ai != nil {<br class="">
result.append(ai!)<br class="">
ai = ai!.ai_next?.pointee<br class="">
}<br class="">
return result<br class="">
}<br class="">
<br class="">
I don't feel that the function is correct, though.<br class="">
<br class="">
• How can the Swift memory model know that getaddrinfo allocates memory, and that Swift should not overwrite that memory with own stuff?<br class="">
• 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 class="">
<br class="">
What's the correct way to interface with getaddrinfo?<br class="">
<br class="">
Thanks<br class="">
<br class="">
Etan<br class="">
<br class="">
<br class="">
______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailma<wbr class="">n/listinfo/swift-users</a><br class="">
</blockquote>
</blockquote>
_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
https://lists.swift.org/mailman/listinfo/swift-users<br class="">
</div>
</blockquote>
</div>
<br class="">
</body>
</html>