Skip to content

Commit

Permalink
Merge pull request #24 from orchetect/dev
Browse files Browse the repository at this point in the history
Fixed retain cycle
  • Loading branch information
orchetect authored Aug 5, 2021
2 parents 0e1b66e + a726939 commit 1e30728
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
44 changes: 16 additions & 28 deletions Sources/MIDIKit/IO/Managed/Input.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@ extension MIDI.IO {

internal var receiveHandler: ReceiveHandler

@inline(__always) private func midiReadBlock(
_ packetListPtr: UnsafePointer<MIDIPacketList>,
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

midiManager?.queue.async {
self.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon)
}

}

@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
@inline(__always) private func midiReceiveBlock(
_ eventListPtr: UnsafePointer<MIDIEventList>,
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

midiManager?.queue.async {
self.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon)
}

}

internal init(name: String,
uniqueID: MIDI.IO.InputEndpoint.UniqueID? = nil,
receiveHandler: ReceiveHandler.Definition,
Expand All @@ -60,7 +37,7 @@ extension MIDI.IO {

deinit {

try? dispose()
_ = try? dispose()

}

Expand Down Expand Up @@ -106,7 +83,13 @@ extension MIDI.IO.Input {
endpointName as CFString,
._1_0,
&newPortRef,
midiReceiveBlock
{ [weak self] eventListPtr, srcConnRefCon in
guard let self = self else { return }
self.midiManager?.queue.async {
self.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon)
}

}
)
.throwIfOSStatusErr()
} else {
Expand All @@ -116,16 +99,21 @@ extension MIDI.IO.Input {
manager.clientRef,
endpointName as CFString,
&newPortRef,
midiReadBlock
{ [weak self] packetListPtr, srcConnRefCon in
guard let self = self else { return }
self.midiManager?.queue.async {
self.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon)
}
}
)
.throwIfOSStatusErr()
}

portRef = newPortRef

// set meta data properties; ignore errors in case of failure
try? MIDI.IO.setModel(of: newPortRef, to: manager.model)
try? MIDI.IO.setManufacturer(of: newPortRef, to: manager.manufacturer)
_ = try? MIDI.IO.setModel(of: newPortRef, to: manager.model)
_ = try? MIDI.IO.setManufacturer(of: newPortRef, to: manager.manufacturer)

if let uniqueID = self.uniqueID {
// inject previously-stored unique ID into port
Expand Down
37 changes: 12 additions & 25 deletions Sources/MIDIKit/IO/Managed/InputConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,6 @@ extension MIDI.IO {

public private(set) var isConnected: Bool = false

@inline(__always) private func midiReadBlock(
_ packetListPtr: UnsafePointer<MIDIPacketList>,
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

midiManager?.queue.async {
self.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon)
}

}

@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
@inline(__always) private func midiReceiveBlock(
_ eventListPtr: UnsafePointer<MIDIEventList>,
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

midiManager?.queue.async {
self.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon)
}

}

internal init(toOutput: MIDI.IO.EndpointIDCriteria<MIDI.IO.OutputEndpoint>,
receiveHandler: ReceiveHandler.Definition,
midiManager: MIDI.IO.Manager) {
Expand Down Expand Up @@ -104,7 +81,12 @@ extension MIDI.IO.InputConnection {
UUID().uuidString as CFString,
._1_0,
&newConnection,
midiReceiveBlock
{ [weak self] eventListPtr, srcConnRefCon in
guard let self = self else { return }
self.midiManager?.queue.async {
self.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon)
}
}
)
.throwIfOSStatusErr()
} else {
Expand All @@ -114,7 +96,12 @@ extension MIDI.IO.InputConnection {
manager.clientRef,
UUID().uuidString as CFString,
&newConnection,
midiReadBlock
{ [weak self] packetListPtr, srcConnRefCon in
guard let self = self else { return }
self.midiManager?.queue.async {
self.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon)
}
}
)
.throwIfOSStatusErr()
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/MIDIKit/IO/Managed/Output.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ extension MIDI.IO.Output {
portRef = newPortRef

// set meta data properties; ignore errors in case of failure
try? MIDI.IO.setModel(of: newPortRef, to: manager.model)
try? MIDI.IO.setManufacturer(of: newPortRef, to: manager.manufacturer)
_ = try? MIDI.IO.setModel(of: newPortRef, to: manager.model)
_ = try? MIDI.IO.setManufacturer(of: newPortRef, to: manager.manufacturer)

if let uniqueID = self.uniqueID {
// inject previously-stored unique ID into port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class InputsAndOutputs_Output_Tests: XCTestCase {
// send a midi message

XCTAssertNotNil(
try? manager.managedOutputs[tag1]!.send(rawMessage: [0xFF])
_ = try? manager.managedOutputs[tag1]!.send(rawMessage: [0xFF])
)

// unique ID collision
Expand Down

0 comments on commit 1e30728

Please sign in to comment.