Skip to content

Commit 44c2ee6

Browse files
authored
Update RtMidi.cpp
Fix possible memory management bugs. Replace one call to CFStringCreateWithCString with just the CFSTR macro.
1 parent 806e18f commit 44c2ee6

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

RtMidi.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ unsigned int MidiInCore :: getPortCount()
11791179

11801180
// This function was submitted by Douglas Casey Tucker and apparently
11811181
// derived largely from PortMidi.
1182-
CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal )
1182+
CFStringRef CreateEndpointName( MIDIEndpointRef endpoint, bool isExternal )
11831183
{
11841184
CFMutableStringRef result = CFStringCreateMutable( NULL, 0 );
11851185
CFStringRef str;
@@ -1189,13 +1189,10 @@ CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal )
11891189
MIDIObjectGetStringProperty( endpoint, kMIDIPropertyName, &str );
11901190
if ( str != NULL ) {
11911191
CFStringAppend( result, str );
1192-
CFRelease( str );
11931192
}
11941193

11951194
// some MIDI devices have a leading space in endpoint name. trim
1196-
CFStringRef space = CFStringCreateWithCString(NULL, " ", kCFStringEncodingUTF8);
1197-
CFStringTrim(result, space);
1198-
CFRelease(space);
1195+
CFStringTrim(result, CFSTR(" "));
11991196

12001197
MIDIEntityRef entity = 0;
12011198
MIDIEndpointGetEntity( endpoint, &entity );
@@ -1209,7 +1206,6 @@ CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal )
12091206
MIDIObjectGetStringProperty( entity, kMIDIPropertyName, &str );
12101207
if ( str != NULL ) {
12111208
CFStringAppend( result, str );
1212-
CFRelease( str );
12131209
}
12141210
}
12151211
// now consider the device's name
@@ -1222,17 +1218,18 @@ CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal )
12221218
MIDIObjectGetStringProperty( device, kMIDIPropertyName, &str );
12231219
if ( CFStringGetLength( result ) == 0 ) {
12241220
CFRelease( result );
1221+
CFRetain( str );
12251222
return str;
12261223
}
12271224
if ( str != NULL ) {
12281225
// if an external device has only one entity, throw away
12291226
// the endpoint name and just use the device name
12301227
if ( isExternal && MIDIDeviceGetNumberOfEntities( device ) < 2 ) {
12311228
CFRelease( result );
1229+
CFRetain( str );
12321230
return str;
12331231
} else {
12341232
if ( CFStringGetLength( str ) == 0 ) {
1235-
CFRelease( str );
12361233
return result;
12371234
}
12381235
// does the entity name already start with the device name?
@@ -1247,15 +1244,14 @@ CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal )
12471244

12481245
CFStringInsert( result, 0, str );
12491246
}
1250-
CFRelease( str );
12511247
}
12521248
}
12531249
return result;
12541250
}
12551251

12561252
// This function was submitted by Douglas Casey Tucker and apparently
12571253
// derived largely from PortMidi.
1258-
static CFStringRef ConnectedEndpointName( MIDIEndpointRef endpoint )
1254+
static CFStringRef CreateConnectedEndpointName( MIDIEndpointRef endpoint )
12591255
{
12601256
CFMutableStringRef result = CFStringCreateMutable( NULL, 0 );
12611257
CFStringRef str;
@@ -1282,11 +1278,12 @@ static CFStringRef ConnectedEndpointName( MIDIEndpointRef endpoint )
12821278
if ( connObjectType == kMIDIObjectType_ExternalSource ||
12831279
connObjectType == kMIDIObjectType_ExternalDestination ) {
12841280
// Connected to an external device's endpoint (10.3 and later).
1285-
str = EndpointName( (MIDIEndpointRef)(connObject), true );
1281+
str = CreateEndpointName( (MIDIEndpointRef)(connObject), true );
12861282
} else {
12871283
// Connected to an external device (10.2) (or something else, catch-
12881284
str = NULL;
12891285
MIDIObjectGetStringProperty( connObject, kMIDIPropertyName, &str );
1286+
if ( str ) CFRetain ( str );
12901287
}
12911288
if ( str != NULL ) {
12921289
if ( anyStrings )
@@ -1307,7 +1304,7 @@ static CFStringRef ConnectedEndpointName( MIDIEndpointRef endpoint )
13071304
CFRelease( result );
13081305

13091306
// Here, either the endpoint had no connections, or we failed to obtain names
1310-
return EndpointName( endpoint, false );
1307+
return CreateEndpointName( endpoint, false );
13111308
}
13121309

13131310
std::string MidiInCore :: getPortName( unsigned int portNumber )
@@ -1327,7 +1324,7 @@ std::string MidiInCore :: getPortName( unsigned int portNumber )
13271324
}
13281325

13291326
portRef = MIDIGetSource( portNumber );
1330-
nameRef = ConnectedEndpointName( portRef );
1327+
nameRef = CreateConnectedEndpointName( portRef );
13311328
CFStringGetCString( nameRef, name, sizeof(name), kCFStringEncodingUTF8 );
13321329
CFRelease( nameRef );
13331330

@@ -1414,7 +1411,7 @@ std::string MidiOutCore :: getPortName( unsigned int portNumber )
14141411
}
14151412

14161413
portRef = MIDIGetDestination( portNumber );
1417-
nameRef = ConnectedEndpointName(portRef);
1414+
nameRef = CreateConnectedEndpointName(portRef);
14181415
CFStringGetCString( nameRef, name, sizeof(name), kCFStringEncodingUTF8 );
14191416
CFRelease( nameRef );
14201417

0 commit comments

Comments
 (0)