Skip to content

Conversation

@kmahar
Copy link
Contributor

@kmahar kmahar commented Apr 13, 2019

This fixes several memory leaks in the driver. Kudos to @valeriomazzeo for pointing these out in #236.

I've done the following:

  • Rename Document(fromPointer:) and DocumentStorage(fromPointer:) to Document(copying:) and DocumentStorage(copying:) to make it clearer what their behavior is
  • Add a new DocumentStorage(stealing:) initializer that takes over responsibility for the provided bson_t, and a Document(stealing:) initializer that calls through to it
  • Update every place the fromPointer initializers were used to call through to the correct new methods

I also fixed a leak of the mongoc_uri_t in the MongoClient initializer, and a leak we'd previously identified in SDAM monitoring (SWIFT-294).

@kmahar kmahar requested review from mbroadst and patrickfreed April 13, 2019 23:47
}

internal init(fromPointer pointer: BSONPointer) {
internal init(copying pointer: BSONPointer) {
Copy link
Member

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

Copy link
Contributor Author

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`.
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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

fatalError("mongoc_cursor_next returned true, but document is nil")
}

// we have to copy because libmongoc owns the pointer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange formatting here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, fixed

Copy link
Contributor

@patrickfreed patrickfreed left a 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) {
Copy link
Contributor

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. 👍

@kmahar kmahar merged commit 6712b1e into master Apr 15, 2019
@kmahar kmahar deleted the SWIFT-394/leaks branch April 15, 2019 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants