-
Notifications
You must be signed in to change notification settings - Fork 24
Fix continuation memory leak in Ares.query #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,8 +160,12 @@ class Ares { | |
preconditionFailure("'arg' is nil. This is a bug.") | ||
} | ||
|
||
let handler = QueryReplyHandler(pointer: handlerPointer) | ||
defer { handlerPointer.deallocate() } | ||
let pointer = handlerPointer.assumingMemoryBound(to: QueryReplyHandler.self) | ||
let handler = pointer.pointee | ||
defer { | ||
pointer.deinitialize(count: 1) | ||
pointer.deallocate() | ||
} | ||
|
||
handler.handle(status: status, buffer: buf, length: len) | ||
} | ||
|
@@ -258,7 +262,7 @@ extension Ares { | |
|
||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
extension Ares { | ||
struct QueryReplyHandler { | ||
class QueryReplyHandler { | ||
private let _handler: (CInt, UnsafeMutablePointer<CUnsignedChar>?, CInt) -> Void | ||
|
||
init<Parser: AresQueryReplyParser>(parser: Parser, _ continuation: CheckedContinuation<Parser.Reply, Error>) { | ||
|
@@ -276,11 +280,6 @@ extension Ares { | |
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely, just pushed a new commit to reflect those changes. I'm interested in understanding the rationale behind class vs struct here. Could you please share more about your thought process? Swift noob eager to learn here 😄 My best guess is that class deinit can free up its members, though my tests indicate no leaks with struct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was told that the address of a struct is not stable (we were thinking deallocate might not be working on the right address because of this), plus we are doing all these pointer manipulations already so class probably makes more sense. |
||
} | ||
|
||
init(pointer: UnsafeMutableRawPointer) { | ||
let handlerPointer = pointer.assumingMemoryBound(to: Self.self) | ||
self = handlerPointer.pointee | ||
} | ||
|
||
func handle(status: CInt, buffer: UnsafeMutablePointer<CUnsignedChar>?, length: CInt) { | ||
self._handler(status, buffer, length) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,18 +115,25 @@ struct DNSSD { | |
byteCount: MemoryLayout<QueryReplyHandler>.stride, | ||
alignment: MemoryLayout<QueryReplyHandler>.alignment | ||
) | ||
// The handler might be called multiple times so don't deallocate inside `callback` | ||
defer { handlerPointer.deallocate() } | ||
|
||
handlerPointer.initializeMemory(as: QueryReplyHandler.self, repeating: handler, count: 1) | ||
|
||
// The handler might be called multiple times so don't deallocate inside `callback` | ||
defer { | ||
let pointer = handlerPointer.assumingMemoryBound(to: QueryReplyHandler.self) | ||
pointer.deinitialize(count: 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
pointer.deallocate() | ||
} | ||
|
||
// This is called once per record received | ||
let callback: DNSServiceQueryRecordReply = { _, _, _, errorCode, _, _, _, rdlen, rdata, _, context in | ||
guard let handlerPointer = context else { | ||
preconditionFailure("'context' is nil. This is a bug.") | ||
} | ||
|
||
let handler = QueryReplyHandler(pointer: handlerPointer) | ||
let pointer = handlerPointer.assumingMemoryBound(to: QueryReplyHandler.self) | ||
let handler = pointer.pointee | ||
|
||
// This parses a record then adds it to the stream | ||
handler.handleRecord(errorCode: errorCode, data: rdata, length: rdlen) | ||
} | ||
|
@@ -171,7 +178,7 @@ struct DNSSD { | |
|
||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
extension DNSSD { | ||
struct QueryReplyHandler { | ||
class QueryReplyHandler { | ||
private let _handleRecord: (DNSServiceErrorType, UnsafeRawPointer?, UInt16) -> Void | ||
|
||
init<Handler: DNSSDQueryReplyHandler>(handler: Handler, _ continuation: AsyncThrowingStream<Handler.Record, Error>.Continuation) { | ||
|
@@ -189,11 +196,6 @@ extension DNSSD { | |
} | ||
} | ||
|
||
init(pointer: UnsafeMutableRawPointer) { | ||
let handlerPointer = pointer.assumingMemoryBound(to: Self.self) | ||
self = handlerPointer.pointee | ||
} | ||
|
||
func handleRecord(errorCode: DNSServiceErrorType, data: UnsafeRawPointer?, length: UInt16) { | ||
self._handleRecord(errorCode, data, length) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I was making similar changes as well but was missing this line of code.