-
Notifications
You must be signed in to change notification settings - Fork 67
SWIFT-294, SWIFT-394 Fix memory leaks #237
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
Conversation
| } | ||
|
|
||
| internal init(fromPointer pointer: BSONPointer) { | ||
| internal init(copying pointer: BSONPointer) { |
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.
Consider adding documentation here. I think you tend to do it at the call site, but it's probably useful to future readers why these are distinguished
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.
added docs for all three DocumentStorage initializers.
| * of `bsonData`, so the caller is responsible for freeing the original | ||
| * memory. | ||
| * Initializes a `Document` from a pointer to a `bson_t` by making a copy of the | ||
| * data. The caller is responsible for freeing the original `bson_t`. |
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.
I don't think this is always correct. For instance, in the APM code above we copy the pointer, and are not responsible for freeing the original bson_t
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.
good point, I updated it to just say that whoever owns the bson_t is responsible for freeing it.
| } | ||
|
|
||
| internal init(stealing pointer: MutableBSONPointer) { | ||
| self.pointer = pointer |
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.
what if self.pointer is already set?
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.
since this is an initializer, I don't think that is possible.
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.
that's a... really good point 😄 I'm like a hammer looking for nails over here
Sources/MongoSwift/MongoCursor.swift
Outdated
| fatalError("mongoc_cursor_next returned true, but document is nil") | ||
| } | ||
|
|
||
| // we have to copy because libmongoc owns the pointer. |
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.
strange formatting here
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.
oops, fixed
patrickfreed
left a comment
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.
Everything, looks good, just one question on some removed code
| * | ||
| * - Returns: a new `Document` | ||
| */ | ||
| internal init(copying pointer: BSONPointer) { |
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.
I really like the copying / stealing labels; it makes it super clear what is going on without needing to refer to the docs. 👍
This fixes several memory leaks in the driver. Kudos to @valeriomazzeo for pointing these out in #236.
I've done the following:
Document(fromPointer:)andDocumentStorage(fromPointer:)toDocument(copying:)andDocumentStorage(copying:)to make it clearer what their behavior isDocumentStorage(stealing:)initializer that takes over responsibility for the providedbson_t, and aDocument(stealing:)initializer that calls through to itfromPointerinitializers were used to call through to the correct new methodsI also fixed a leak of the
mongoc_uri_tin theMongoClientinitializer, and a leak we'd previously identified in SDAM monitoring (SWIFT-294).