Fix potential memory leaks if CoreAudio initialization fails.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 28 Oct 2006 07:44:38 +0000
changeset 205572cc2226d6a3
parent 2054 d22372343744
child 2056 ba0a03843db4
Fix potential memory leaks if CoreAudio initialization fails.
src/audio/macosx/SDL_coreaudio.c
src/audio/macosx/SDL_coreaudio.h
     1.1 --- a/src/audio/macosx/SDL_coreaudio.c	Sat Oct 28 07:39:35 2006 +0000
     1.2 +++ b/src/audio/macosx/SDL_coreaudio.c	Sat Oct 28 07:44:38 2006 +0000
     1.3 @@ -315,26 +315,29 @@
     1.4  COREAUDIO_CloseDevice(_THIS)
     1.5  {
     1.6      if (this->hidden != NULL) {
     1.7 -        OSStatus result = noErr;
     1.8 -        AURenderCallbackStruct callback;
     1.9 -        const AudioUnitElement output_bus = 0;
    1.10 -        const AudioUnitElement input_bus = 1;
    1.11 -        const int iscapture = this->iscapture;
    1.12 -        const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
    1.13 -        const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
    1.14 -                                                    kAudioUnitScope_Input);
    1.15 +        if (this->hidden->audioUnitOpened) {
    1.16 +            OSStatus result = noErr;
    1.17 +            AURenderCallbackStruct callback;
    1.18 +            const AudioUnitElement output_bus = 0;
    1.19 +            const AudioUnitElement input_bus = 1;
    1.20 +            const int iscapture = this->iscapture;
    1.21 +            const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
    1.22 +            const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
    1.23 +                                                        kAudioUnitScope_Input);
    1.24  
    1.25 -        /* stop processing the audio unit */
    1.26 -        result = AudioOutputUnitStop(this->hidden->audioUnit);
    1.27 +            /* stop processing the audio unit */
    1.28 +            result = AudioOutputUnitStop(this->hidden->audioUnit);
    1.29  
    1.30 -        /* Remove the input callback */
    1.31 -        SDL_memset(&callback, '\0', sizeof (AURenderCallbackStruct));
    1.32 -        result = AudioUnitSetProperty(this->hidden->audioUnit,
    1.33 -                                      kAudioUnitProperty_SetRenderCallback,
    1.34 -                                      scope, bus, &callback, sizeof (callback));
    1.35 +            /* Remove the input callback */
    1.36 +            SDL_memset(&callback, '\0', sizeof (AURenderCallbackStruct));
    1.37 +            result = AudioUnitSetProperty(this->hidden->audioUnit,
    1.38 +                                          kAudioUnitProperty_SetRenderCallback,
    1.39 +                                          scope, bus, &callback,
    1.40 +                                          sizeof (callback));
    1.41  
    1.42 -        CloseComponent(this->hidden->audioUnit);
    1.43 -
    1.44 +            CloseComponent(this->hidden->audioUnit);
    1.45 +            this->hidden->audioUnitOpened = 0;
    1.46 +        }
    1.47          SDL_free(this->hidden->buffer);
    1.48          SDL_free(this->hidden);
    1.49          this->hidden = NULL;
    1.50 @@ -435,6 +438,8 @@
    1.51      result = OpenAComponent(comp, &this->hidden->audioUnit);
    1.52      CHECK_RESULT("OpenAComponent");
    1.53  
    1.54 +    this->hidden->audioUnitOpened = 1;
    1.55 +
    1.56      // !!! FIXME: this is wrong?
    1.57      enableIO = ((iscapture) ? 1 : 0);
    1.58      result = AudioUnitSetProperty(this->hidden->audioUnit,
    1.59 @@ -544,6 +549,7 @@
    1.60      }
    1.61  
    1.62      if (!valid_datatype) {      /* shouldn't happen, but just in case... */
    1.63 +        COREAUDIO_CloseDevice(this);
    1.64          SDL_SetError("Unsupported audio format");
    1.65          return 0;
    1.66      }
    1.67 @@ -554,6 +560,7 @@
    1.68          strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
    1.69  
    1.70      if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
    1.71 +        COREAUDIO_CloseDevice(this); \
    1.72          return 0;  /* prepare_audiounit() will call SDL_SetError()... */
    1.73      }
    1.74  
     2.1 --- a/src/audio/macosx/SDL_coreaudio.h	Sat Oct 28 07:39:35 2006 +0000
     2.2 +++ b/src/audio/macosx/SDL_coreaudio.h	Sat Oct 28 07:44:38 2006 +0000
     2.3 @@ -32,6 +32,7 @@
     2.4  struct SDL_PrivateAudioData
     2.5  {
     2.6      AudioUnit audioUnit;
     2.7 +    int audioUnitInitialized;
     2.8      void *buffer;
     2.9      UInt32 bufferOffset;
    2.10      UInt32 bufferSize;