comparison src/audio/macosx/SDL_coreaudio.c @ 2055:72cc2226d6a3

Fix potential memory leaks if CoreAudio initialization fails.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 28 Oct 2006 07:44:38 +0000
parents 5f6550e5184f
children c2fed8bb6700
comparison
equal deleted inserted replaced
2054:d22372343744 2055:72cc2226d6a3
313 313
314 static void 314 static void
315 COREAUDIO_CloseDevice(_THIS) 315 COREAUDIO_CloseDevice(_THIS)
316 { 316 {
317 if (this->hidden != NULL) { 317 if (this->hidden != NULL) {
318 OSStatus result = noErr; 318 if (this->hidden->audioUnitOpened) {
319 AURenderCallbackStruct callback; 319 OSStatus result = noErr;
320 const AudioUnitElement output_bus = 0; 320 AURenderCallbackStruct callback;
321 const AudioUnitElement input_bus = 1; 321 const AudioUnitElement output_bus = 0;
322 const int iscapture = this->iscapture; 322 const AudioUnitElement input_bus = 1;
323 const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus); 323 const int iscapture = this->iscapture;
324 const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output : 324 const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
325 kAudioUnitScope_Input); 325 const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
326 326 kAudioUnitScope_Input);
327 /* stop processing the audio unit */ 327
328 result = AudioOutputUnitStop(this->hidden->audioUnit); 328 /* stop processing the audio unit */
329 329 result = AudioOutputUnitStop(this->hidden->audioUnit);
330 /* Remove the input callback */ 330
331 SDL_memset(&callback, '\0', sizeof (AURenderCallbackStruct)); 331 /* Remove the input callback */
332 result = AudioUnitSetProperty(this->hidden->audioUnit, 332 SDL_memset(&callback, '\0', sizeof (AURenderCallbackStruct));
333 kAudioUnitProperty_SetRenderCallback, 333 result = AudioUnitSetProperty(this->hidden->audioUnit,
334 scope, bus, &callback, sizeof (callback)); 334 kAudioUnitProperty_SetRenderCallback,
335 335 scope, bus, &callback,
336 CloseComponent(this->hidden->audioUnit); 336 sizeof (callback));
337 337
338 CloseComponent(this->hidden->audioUnit);
339 this->hidden->audioUnitOpened = 0;
340 }
338 SDL_free(this->hidden->buffer); 341 SDL_free(this->hidden->buffer);
339 SDL_free(this->hidden); 342 SDL_free(this->hidden);
340 this->hidden = NULL; 343 this->hidden = NULL;
341 } 344 }
342 } 345 }
432 } 435 }
433 436
434 /* Open & initialize the audio unit */ 437 /* Open & initialize the audio unit */
435 result = OpenAComponent(comp, &this->hidden->audioUnit); 438 result = OpenAComponent(comp, &this->hidden->audioUnit);
436 CHECK_RESULT("OpenAComponent"); 439 CHECK_RESULT("OpenAComponent");
440
441 this->hidden->audioUnitOpened = 1;
437 442
438 // !!! FIXME: this is wrong? 443 // !!! FIXME: this is wrong?
439 enableIO = ((iscapture) ? 1 : 0); 444 enableIO = ((iscapture) ? 1 : 0);
440 result = AudioUnitSetProperty(this->hidden->audioUnit, 445 result = AudioUnitSetProperty(this->hidden->audioUnit,
441 kAudioOutputUnitProperty_EnableIO, 446 kAudioOutputUnitProperty_EnableIO,
542 break; 547 break;
543 } 548 }
544 } 549 }
545 550
546 if (!valid_datatype) { /* shouldn't happen, but just in case... */ 551 if (!valid_datatype) { /* shouldn't happen, but just in case... */
552 COREAUDIO_CloseDevice(this);
547 SDL_SetError("Unsupported audio format"); 553 SDL_SetError("Unsupported audio format");
548 return 0; 554 return 0;
549 } 555 }
550 556
551 strdesc.mBytesPerFrame = 557 strdesc.mBytesPerFrame =
552 strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8; 558 strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
553 strdesc.mBytesPerPacket = 559 strdesc.mBytesPerPacket =
554 strdesc.mBytesPerFrame * strdesc.mFramesPerPacket; 560 strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
555 561
556 if (!prepare_audiounit(this, devname, iscapture, &strdesc)) { 562 if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
563 COREAUDIO_CloseDevice(this); \
557 return 0; /* prepare_audiounit() will call SDL_SetError()... */ 564 return 0; /* prepare_audiounit() will call SDL_SetError()... */
558 } 565 }
559 566
560 return 1; /* good to go. */ 567 return 1; /* good to go. */
561 } 568 }