Skip to content

[3.1] Fix for memory leak in NSUUID #948

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

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Foundation/NSUUID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@ import CoreFoundation

open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
internal var buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)

deinit {
buffer.deinitialize()
buffer.deallocate(capacity: 16)
}

public override init() {
_cf_uuid_generate_random(buffer)
}

public convenience init?(uuidString string: String) {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
defer {
buffer.deinitialize()
buffer.deallocate(capacity: 16)
}
if _cf_uuid_parse(string, buffer) != 0 {
return nil
}
Expand All @@ -41,6 +50,10 @@ open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {

open var uuidString: String {
let strPtr = UnsafeMutablePointer<Int8>.allocate(capacity: 37)
defer {
strPtr.deinitialize()
strPtr.deallocate(capacity: 37)
}
_cf_uuid_unparse_upper(buffer, strPtr)
return String(cString: strPtr)
}
Expand Down Expand Up @@ -69,6 +82,10 @@ open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
guard let data = decodedData else { return nil }
guard data.count == 16 else { return nil }
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
defer {
buffer.deinitialize()
buffer.deallocate(capacity: 16)
}
data.copyBytes(to: buffer, count: 16)
self.init(uuidBytes: buffer)
}
Expand Down