-
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
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 |
|---|---|---|
|
|
@@ -20,14 +20,22 @@ public class DocumentStorage { | |
| return Int(bson_count_keys(self.pointer)) | ||
| } | ||
|
|
||
| /// Initializes a new, empty `DocumentStorage`. | ||
| internal init() { | ||
| self.pointer = bson_new() | ||
| } | ||
|
|
||
| internal init(fromPointer pointer: BSONPointer) { | ||
| /// Initializes a new `DocumentStorage` by copying over the data from the provided `bson_t`. | ||
| internal init(copying pointer: BSONPointer) { | ||
| self.pointer = bson_copy(pointer) | ||
| } | ||
|
|
||
| /// Initializes a new `DocumentStorage` that uses the provided `bson_t` as its backing storage. | ||
| /// The newly created instance will handle cleaning up the pointer upon deinitialization. | ||
| internal init(stealing pointer: MutableBSONPointer) { | ||
| self.pointer = pointer | ||
|
Member
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. what if
Contributor
Author
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. since this is an initializer, I don't think that is possible.
Member
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. that's a... really good point 😄 I'm like a hammer looking for nails over here |
||
| } | ||
|
|
||
| /// Cleans up internal state. | ||
| deinit { | ||
| guard let pointer = self.pointer else { | ||
|
|
@@ -57,17 +65,30 @@ extension Document { | |
| } | ||
|
|
||
| /** | ||
| * Initializes a `Document` from a pointer to a bson_t. Uses a copy | ||
| * 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 `bson_t`'s owner is | ||
| * responsible for freeing the original. | ||
| * | ||
| * - Parameters: | ||
| * - pointer: a BSONPointer | ||
| * | ||
| * - Returns: a new `Document` | ||
| */ | ||
| internal init(copying pointer: BSONPointer) { | ||
|
Contributor
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 really like the |
||
| self.storage = DocumentStorage(copying: pointer) | ||
| self.count = self.storage.count | ||
| } | ||
|
|
||
| /** | ||
| * Initializes a `Document` from a pointer to a `bson_t`, "stealing" the `bson_t` to use for underlying storage/ | ||
| * The `bson_t` must not be modified or freed by others after it is used here/ | ||
| * | ||
| * - Parameters: | ||
| * - fromPointer: a BSONPointer | ||
| * - pointer: a MutableBSONPointer | ||
| * | ||
| * - Returns: a new `Document` | ||
| */ | ||
| internal init(fromPointer pointer: BSONPointer) { | ||
| self.storage = DocumentStorage(fromPointer: pointer) | ||
| internal init(stealing pointer: MutableBSONPointer) { | ||
| self.storage = DocumentStorage(stealing: pointer) | ||
| self.count = self.storage.count | ||
| } | ||
|
|
||
|
|
@@ -232,7 +253,7 @@ extension Document { | |
| */ | ||
| private mutating func copyStorageIfRequired() { | ||
| if !isKnownUniquelyReferenced(&self.storage) { | ||
| self.storage = DocumentStorage(fromPointer: self.data) | ||
| self.storage = DocumentStorage(copying: self.data) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -319,7 +340,7 @@ extension Document { | |
| * - A `UserError.invalidArgumentError` if the data passed in is invalid JSON. | ||
| */ | ||
| public init(fromJSON: Data) throws { | ||
| self.storage = DocumentStorage(fromPointer: try fromJSON.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in | ||
| self.storage = DocumentStorage(stealing: try fromJSON.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in | ||
| var error = bson_error_t() | ||
| guard let bson = bson_new_from_json(bytes, fromJSON.count, &error) else { | ||
| if error.domain == BSON_ERROR_JSON { | ||
|
|
@@ -328,11 +349,7 @@ extension Document { | |
| throw RuntimeError.internalError(message: toErrorString(error)) | ||
| } | ||
|
|
||
| #if compiler(>=5.0) | ||
patrickfreed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return bson | ||
| #else | ||
| return UnsafePointer(bson) | ||
| #endif | ||
| }) | ||
| self.count = self.storage.count | ||
| } | ||
|
|
@@ -348,7 +365,7 @@ extension Document { | |
|
|
||
| /// Constructs a `Document` from raw BSON `Data`. | ||
| public init(fromBSON: Data) { | ||
| self.storage = DocumentStorage(fromPointer: fromBSON.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in | ||
| self.storage = DocumentStorage(stealing: fromBSON.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) in | ||
| bson_new_from_data(bytes, fromBSON.count) | ||
| }) | ||
| self.count = self.storage.count | ||
|
|
@@ -456,7 +473,7 @@ extension Document: BSONValue { | |
| throw RuntimeError.internalError(message: "Failed to create a Document from iterator") | ||
| } | ||
|
|
||
| return self.init(fromPointer: docData) | ||
| return self.init(stealing: docData) | ||
| } | ||
| } | ||
| } | ||
|
|
||
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
DocumentStorageinitializers.