Skip to content
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

Fix memory safety issue #1

Merged
merged 1 commit into from
Feb 14, 2024
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
29 changes: 27 additions & 2 deletions Sources/Mockingjay/MockingjayProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,35 @@ public func ==(lhs:Stub, rhs:Stub) -> Bool {
return lhs.uuid == rhs.uuid
}

var stubs = [Stub]()
var registered: Bool = false
class MockingjayStorage {
static let shared = MockingjayStorage()

var stubs: [Stub] {
get { accessQueue.sync { _stubs } }
set { accessQueue.sync { _stubs = newValue } }
}

var registered: Bool {
get { accessQueue.sync { _registered } }
set { accessQueue.sync { _registered = newValue } }
}

private var _stubs = [Stub]()
private var _registered: Bool = false
private var accessQueue: DispatchQueue { DispatchQueue(label: "MockingjayStorageQueue", qos: .default) }
}
Comment on lines +31 to +47

Choose a reason for hiding this comment

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

Suggested change
class MockingjayStorage {
static let shared = MockingjayStorage()
var stubs: [Stub] {
get { accessQueue.sync { _stubs } }
set { accessQueue.sync { _stubs = newValue } }
}
var registered: Bool {
get { accessQueue.sync { _registered } }
set { accessQueue.sync { _registered = newValue } }
}
private var _stubs = [Stub]()
private var _registered: Bool = false
private var accessQueue: DispatchQueue { DispatchQueue(label: "MockingjayStorageQueue", qos: .default) }
}
class MockingjayStorage {
static let shared = MockingjayStorage()
var stubs: [Stub] {
get { accessQueue.sync { _stubs } }
set { accessQueue.sync { _stubs = newValue } }
}
var registered: Bool {
get { accessQueue.sync { _registered } }
set { accessQueue.sync { _registered = newValue } }
}
private var _stubs = [Stub]()
private var _registered: Bool = false
private var accessQueue: DispatchQueue { DispatchQueue(label: "MockingjayStorageQueue", qos: .default) }
}


public class MockingjayProtocol: URLProtocol {
private class var storage: MockingjayStorage { .shared }
private class var stubs: [Stub] {
get { storage.stubs }
set { storage.stubs = newValue }
}
private class var registered: Bool {
get { storage.registered }
set { storage.registered = newValue }
}

Comment on lines +50 to +59

Choose a reason for hiding this comment

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

Suggested change
private class var storage: MockingjayStorage { .shared }
private class var stubs: [Stub] {
get { storage.stubs }
set { storage.stubs = newValue }
}
private class var registered: Bool {
get { storage.registered }
set { storage.registered = newValue }
}
private class var storage: MockingjayStorage { .shared }
private class var stubs: [Stub] {
get { storage.stubs }
set { storage.stubs = newValue }
}
private class var registered: Bool {
get { storage.registered }
set { storage.registered = newValue }
}

// MARK: Stubs
fileprivate var enableDownloading = true
fileprivate let operationQueue = OperationQueue()
Expand Down