@@ -4,6 +4,8 @@ import CLibMongoC
44internal class ConnectionString {
55 /// Pointer to the underlying `mongoc_uri_t`.
66 private let _uri : OpaquePointer
7+ /// Tracks whether we have already destroyed the above pointer.
8+ private var destroyedPtr = false
79
810 /// Initializes a new `ConnectionString` with the provided options.
911 internal init ( _ connectionString: String , options: MongoClientOptions ? = nil ) throws {
@@ -13,7 +15,16 @@ internal class ConnectionString {
1315 }
1416 self . _uri = uri
1517
16- try self . applyAndValidateOptions ( options)
18+ // Due to SR-13355, deinit does not get called if we throw here. If we encounter an error, we need to manually
19+ // clean up the pointer. We use a variable to track whether we already destroyed it so that, in the event the
20+ // bug is fixed and `deinit` starts being called, we don't start calling `mongoc_uri_destroy` twice.
21+ do {
22+ try self . applyAndValidateOptions ( options)
23+ } catch {
24+ mongoc_uri_destroy ( self . _uri)
25+ self . destroyedPtr = true
26+ throw error
27+ }
1728 }
1829
1930 private func applyAndValidateOptions( _ options: MongoClientOptions ? ) throws {
@@ -361,7 +372,9 @@ internal class ConnectionString {
361372
362373 /// Cleans up the underlying `mongoc_uri_t`.
363374 deinit {
364- mongoc_uri_destroy ( self . _uri)
375+ if !self . destroyedPtr {
376+ mongoc_uri_destroy ( self . _uri)
377+ }
365378 }
366379
367380 private var usesDNSSeedlistFormat : Bool {
0 commit comments