dynamic_flac.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Jun 2015 00:11:41 -0700
changeset 705 fe757163b8f7
parent 629 87c536d57d92
child 711 f40c5ac95b12
permissions -rw-r--r--
Fixed bug 3018 - Loading MIDI music using FluidSynth leaks memory.

Philipp Wiesemann

There is a memory leak in fluidsynth.c and fluidsynth_loadsong_RW_internal(). The allocated temporary buffer is not deleted if fluid_player_add_mem() returns FLUID_OK.
slouken@382
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@382
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@382
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@382
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@382
    20
slouken@518
    21
  Implementation of the dynamic loading functionality for libFLAC.
slouken@617
    22
    ~ Austen Dicken (admin@cvpcs.org)
slouken@382
    23
*/
slouken@382
    24
slouken@382
    25
#ifdef FLAC_MUSIC
slouken@382
    26
slouken@382
    27
#include "SDL_loadso.h"
slouken@629
    28
#include "SDL_mixer.h"
slouken@382
    29
#include "dynamic_flac.h"
slouken@382
    30
slouken@382
    31
flac_loader flac = {
slouken@617
    32
    0, NULL
slouken@382
    33
};
slouken@382
    34
slouken@382
    35
#ifdef FLAC_DYNAMIC
slouken@470
    36
int Mix_InitFLAC()
slouken@470
    37
{
slouken@617
    38
    if ( flac.loaded == 0 ) {
slouken@617
    39
        flac.handle = SDL_LoadObject(FLAC_DYNAMIC);
slouken@617
    40
        if ( flac.handle == NULL ) {
slouken@617
    41
            return -1;
slouken@617
    42
        }
slouken@617
    43
        flac.FLAC__stream_decoder_new =
slouken@617
    44
            (FLAC__StreamDecoder *(*)())
slouken@617
    45
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_new");
slouken@617
    46
        if ( flac.FLAC__stream_decoder_new == NULL ) {
slouken@617
    47
            SDL_UnloadObject(flac.handle);
slouken@617
    48
            return -1;
slouken@617
    49
        }
slouken@617
    50
        flac.FLAC__stream_decoder_delete =
slouken@617
    51
            (void (*)(FLAC__StreamDecoder *))
slouken@617
    52
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_delete");
slouken@617
    53
        if ( flac.FLAC__stream_decoder_delete == NULL ) {
slouken@617
    54
            SDL_UnloadObject(flac.handle);
slouken@617
    55
            return -1;
slouken@617
    56
        }
slouken@617
    57
        flac.FLAC__stream_decoder_init_stream =
slouken@617
    58
            (FLAC__StreamDecoderInitStatus (*)(
slouken@617
    59
                        FLAC__StreamDecoder *,
slouken@617
    60
                        FLAC__StreamDecoderReadCallback,
slouken@617
    61
                        FLAC__StreamDecoderSeekCallback,
slouken@617
    62
                        FLAC__StreamDecoderTellCallback,
slouken@617
    63
                        FLAC__StreamDecoderLengthCallback,
slouken@617
    64
                        FLAC__StreamDecoderEofCallback,
slouken@617
    65
                        FLAC__StreamDecoderWriteCallback,
slouken@617
    66
                        FLAC__StreamDecoderMetadataCallback,
slouken@617
    67
                        FLAC__StreamDecoderErrorCallback,
slouken@617
    68
                        void *))
slouken@617
    69
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_init_stream");
slouken@617
    70
        if ( flac.FLAC__stream_decoder_init_stream == NULL ) {
slouken@617
    71
            SDL_UnloadObject(flac.handle);
slouken@617
    72
            return -1;
slouken@617
    73
        }
slouken@617
    74
        flac.FLAC__stream_decoder_finish =
slouken@617
    75
            (FLAC__bool (*)(FLAC__StreamDecoder *))
slouken@617
    76
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_finish");
slouken@617
    77
        if ( flac.FLAC__stream_decoder_finish == NULL ) {
slouken@617
    78
            SDL_UnloadObject(flac.handle);
slouken@617
    79
            return -1;
slouken@617
    80
        }
slouken@617
    81
        flac.FLAC__stream_decoder_flush =
slouken@617
    82
            (FLAC__bool (*)(FLAC__StreamDecoder *))
slouken@617
    83
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_flush");
slouken@617
    84
        if ( flac.FLAC__stream_decoder_flush == NULL ) {
slouken@617
    85
            SDL_UnloadObject(flac.handle);
slouken@617
    86
            return -1;
slouken@617
    87
        }
slouken@617
    88
        flac.FLAC__stream_decoder_process_single =
slouken@617
    89
            (FLAC__bool (*)(FLAC__StreamDecoder *))
slouken@617
    90
            SDL_LoadFunction(flac.handle,
slouken@617
    91
                        "FLAC__stream_decoder_process_single");
slouken@617
    92
        if ( flac.FLAC__stream_decoder_process_single == NULL ) {
slouken@617
    93
            SDL_UnloadObject(flac.handle);
slouken@617
    94
            return -1;
slouken@617
    95
        }
slouken@617
    96
        flac.FLAC__stream_decoder_process_until_end_of_metadata =
slouken@617
    97
            (FLAC__bool (*)(FLAC__StreamDecoder *))
slouken@617
    98
            SDL_LoadFunction(flac.handle,
slouken@617
    99
                        "FLAC__stream_decoder_process_until_end_of_metadata");
slouken@617
   100
        if ( flac.FLAC__stream_decoder_process_until_end_of_metadata == NULL ) {
slouken@617
   101
            SDL_UnloadObject(flac.handle);
slouken@617
   102
            return -1;
slouken@617
   103
        }
slouken@617
   104
        flac.FLAC__stream_decoder_process_until_end_of_stream =
slouken@617
   105
            (FLAC__bool (*)(FLAC__StreamDecoder *))
slouken@617
   106
            SDL_LoadFunction(flac.handle,
slouken@617
   107
                        "FLAC__stream_decoder_process_until_end_of_stream");
slouken@617
   108
        if ( flac.FLAC__stream_decoder_process_until_end_of_stream == NULL ) {
slouken@617
   109
            SDL_UnloadObject(flac.handle);
slouken@617
   110
            return -1;
slouken@617
   111
        }
slouken@617
   112
        flac.FLAC__stream_decoder_seek_absolute =
slouken@617
   113
            (FLAC__bool (*)(FLAC__StreamDecoder *, FLAC__uint64))
slouken@617
   114
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_seek_absolute");
slouken@617
   115
        if ( flac.FLAC__stream_decoder_seek_absolute == NULL ) {
slouken@617
   116
            SDL_UnloadObject(flac.handle);
slouken@617
   117
            return -1;
slouken@617
   118
        }
slouken@617
   119
        flac.FLAC__stream_decoder_get_state =
slouken@617
   120
            (FLAC__StreamDecoderState (*)(const FLAC__StreamDecoder *decoder))
slouken@617
   121
            SDL_LoadFunction(flac.handle, "FLAC__stream_decoder_get_state");
slouken@617
   122
        if ( flac.FLAC__stream_decoder_get_state == NULL ) {
slouken@617
   123
            SDL_UnloadObject(flac.handle);
slouken@617
   124
            return -1;
slouken@617
   125
        }
slouken@617
   126
    }
slouken@617
   127
    ++flac.loaded;
slouken@382
   128
slouken@617
   129
    return 0;
slouken@382
   130
}
slouken@470
   131
void Mix_QuitFLAC()
slouken@470
   132
{
slouken@617
   133
    if ( flac.loaded == 0 ) {
slouken@617
   134
        return;
slouken@617
   135
    }
slouken@617
   136
    if ( flac.loaded == 1 ) {
slouken@617
   137
        SDL_UnloadObject(flac.handle);
slouken@617
   138
    }
slouken@617
   139
    --flac.loaded;
slouken@382
   140
}
slouken@382
   141
#else
slouken@470
   142
int Mix_InitFLAC()
slouken@470
   143
{
slouken@617
   144
    if ( flac.loaded == 0 ) {
slouken@629
   145
#ifdef __MACOSX__
slouken@629
   146
        extern FLAC__StreamDecoder *FLAC__stream_decoder_new(void) __attribute__((weak_import));
slouken@629
   147
        if ( FLAC__stream_decoder_new == NULL )
slouken@629
   148
        {
slouken@629
   149
            /* Missing weakly linked framework */
slouken@629
   150
            Mix_SetError("Missing FLAC.framework");
slouken@629
   151
            return -1;
slouken@629
   152
        }
slouken@629
   153
#endif // __MACOSX__
slouken@629
   154
slouken@617
   155
        flac.FLAC__stream_decoder_new = FLAC__stream_decoder_new;
slouken@617
   156
        flac.FLAC__stream_decoder_delete = FLAC__stream_decoder_delete;
slouken@617
   157
        flac.FLAC__stream_decoder_init_stream =
slouken@617
   158
                            FLAC__stream_decoder_init_stream;
slouken@617
   159
        flac.FLAC__stream_decoder_finish = FLAC__stream_decoder_finish;
slouken@617
   160
        flac.FLAC__stream_decoder_flush = FLAC__stream_decoder_flush;
slouken@617
   161
        flac.FLAC__stream_decoder_process_single =
slouken@617
   162
                            FLAC__stream_decoder_process_single;
slouken@617
   163
        flac.FLAC__stream_decoder_process_until_end_of_metadata =
slouken@617
   164
                            FLAC__stream_decoder_process_until_end_of_metadata;
slouken@617
   165
        flac.FLAC__stream_decoder_process_until_end_of_stream =
slouken@617
   166
                            FLAC__stream_decoder_process_until_end_of_stream;
slouken@617
   167
        flac.FLAC__stream_decoder_seek_absolute =
slouken@617
   168
                            FLAC__stream_decoder_seek_absolute;
slouken@617
   169
        flac.FLAC__stream_decoder_get_state =
slouken@617
   170
                            FLAC__stream_decoder_get_state;
slouken@617
   171
    }
slouken@617
   172
    ++flac.loaded;
slouken@382
   173
slouken@617
   174
    return 0;
slouken@382
   175
}
slouken@470
   176
void Mix_QuitFLAC()
slouken@470
   177
{
slouken@617
   178
    if ( flac.loaded == 0 ) {
slouken@617
   179
        return;
slouken@617
   180
    }
slouken@617
   181
    if ( flac.loaded == 1 ) {
slouken@617
   182
    }
slouken@617
   183
    --flac.loaded;
slouken@382
   184
}
slouken@470
   185
#endif /* FLAC_DYNAMIC */
slouken@382
   186
slouken@470
   187
#endif /* FLAC_MUSIC */