Skip to content

Commit 308701b

Browse files
committed
Fixed potential crash in rare cases when Core MIDI property getter produces an error
1 parent be3342e commit 308701b

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

Sources/MIDIKitIO/Core MIDI/Core MIDI Properties Get.swift

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,20 @@ func getProperties(
2626
let result = MIDIObjectGetProperties(ref, &props, deep)
2727

2828
guard result == noErr else {
29-
props?.release()
3029
throw MIDIIOError.osStatus(result)
3130
}
3231

3332
guard let unwrappedProps = props?.takeRetainedValue() else {
34-
props?.release()
3533
throw MIDIIOError.readError(
3634
"Got nil while reading MIDIEndpointRef property list."
3735
)
3836
}
3937

4038
// "takeRetainedValue() is the right choice here because it is the caller's responsibility to
4139
// release the string.
42-
// This is different from the usual Core Foundation memory management rules, but documented in
43-
// the MIDI Services Reference"
44-
// -- https://stackoverflow.com/a/27171498/2805570
40+
// This is different from the usual Core Foundation memory management rules, but documented in
41+
// the MIDI Services Reference."
42+
// -- https://stackoverflow.com/a/27171498/2805570
4543

4644
return unwrappedProps
4745
}
@@ -55,29 +53,27 @@ func getProperties(
5553
///
5654
/// - Throws: ``MIDIIOError``
5755
func getDictionary(
58-
forProperty: CFString,
56+
forProperty property: CFString,
5957
of ref: CoreMIDI.MIDIObjectRef
6058
) throws -> NSDictionary {
6159
var dict: Unmanaged<CFDictionary>?
62-
let result = MIDIObjectGetDictionaryProperty(ref, forProperty, &dict)
60+
let result = MIDIObjectGetDictionaryProperty(ref, property, &dict)
6361

6462
guard result == noErr else {
65-
dict?.release()
6663
throw MIDIIOError.osStatus(result)
6764
}
6865

6966
guard let unwrappedDict = dict?.takeRetainedValue() else {
70-
dict?.release()
7167
throw MIDIIOError.readError(
7268
"Got nil while reading MIDIEndpointRef property list."
7369
)
7470
}
7571

7672
// "takeRetainedValue() is the right choice here because it is the caller's responsibility to
7773
// release the string.
78-
// This is different from the usual Core Foundation memory management rules, but documented in
79-
// the MIDI Services Reference"
80-
// -- https://stackoverflow.com/a/27171498/2805570
74+
// This is different from the usual Core Foundation memory management rules, but documented in
75+
// the MIDI Services Reference."
76+
// -- https://stackoverflow.com/a/27171498/2805570
8177

8278
return unwrappedDict as NSDictionary
8379
}
@@ -91,29 +87,27 @@ func getDictionary(
9187
///
9288
/// - Throws: ``MIDIIOError``
9389
func getString(
94-
forProperty: CFString,
90+
forProperty property: CFString,
9591
of ref: CoreMIDI.MIDIObjectRef
9692
) throws -> String {
9793
var val: Unmanaged<CFString>?
98-
let result = MIDIObjectGetStringProperty(ref, forProperty, &val)
94+
let result = MIDIObjectGetStringProperty(ref, property, &val)
9995

10096
guard result == noErr else {
101-
val?.release()
10297
throw MIDIIOError.osStatus(result)
10398
}
10499

105100
guard let unwrappedVal = val?.takeRetainedValue() else {
106-
val?.release()
107101
throw MIDIIOError.readError(
108-
"Got nil while reading MIDIEndpointRef property value \((forProperty as String).quoted)"
102+
"Got nil while reading MIDIEndpointRef property value \((property as String).quoted)"
109103
)
110104
}
111105

112106
// "takeRetainedValue() is the right choice here because it is the caller's responsibility to
113107
// release the string.
114-
// This is different from the usual Core Foundation memory management rules, but documented in
115-
// the MIDI Services Reference"
116-
// -- https://stackoverflow.com/a/27171498/2805570
108+
// This is different from the usual Core Foundation memory management rules, but documented in
109+
// the MIDI Services Reference."
110+
// -- https://stackoverflow.com/a/27171498/2805570
117111

118112
return unwrappedVal as String
119113
}
@@ -123,11 +117,12 @@ func getString(
123117
///
124118
/// - Parameter forProperty: a `CoreMIDI.Property` constant
125119
func getInteger(
126-
forProperty: CFString,
120+
forProperty property: CFString,
127121
of ref: CoreMIDI.MIDIObjectRef
128122
) -> Int32 {
129123
var val: Int32 = 0
130-
_ = MIDIObjectGetIntegerProperty(ref, forProperty, &val)
124+
// TODO: handle OSStatus errors?
125+
_ = MIDIObjectGetIntegerProperty(ref, property, &val)
131126
return val
132127
}
133128

0 commit comments

Comments
 (0)