-2

I call strdup to duplicate the 'card' string in set_device( devname ) set_device( devname ) and then I use 'card' to open mixer:

devname is in format hw:0/Mic

static char *card, *channel;
static snd_mixer_t *handle = NULL;
static snd_mixer_elem_t *elem = NULL;
static long min, max, vol;

static void open_mixer( void )
{
        int err;
        static snd_mixer_selem_id_t *sid = NULL;
        if ((err = snd_mixer_open (&handle, 0)) < 0) {
            return;
        }
        if ((err = snd_mixer_attach (handle, card)) < 0) {   /* memory leak */
            goto error;
        }
        if ((err = snd_mixer_selem_register (handle, NULL, NULL)) < 0) {
            goto error;
        }
        if ((err = snd_mixer_load (handle)) < 0) {
            goto error;
        }
        snd_mixer_selem_id_malloc(&sid);
        if (sid == NULL)
            goto error;
        snd_mixer_selem_id_set_name(sid, channel);
        if (!(elem = snd_mixer_find_selem(handle, sid))) {
            goto error;
        }
        if (!snd_mixer_selem_has_playback_volume(elem)) {
            goto error;
        }
        snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
        if ((max - min) <= 0) {
            goto error;
        }
        snd_mixer_selem_id_free(sid);
        return;

error:
        if (sid)
            snd_mixer_selem_id_free(sid);
        if (handle) {
            snd_mixer_close(handle);
            handle = NULL;
        }
        return;
}

int set_device( const char *devname )
{
        int i;

        if (card) free(card);
        card = strdup( devname );
        if( !card ) return -1;

        i = strcspn( card, "/" );
        if( i == strlen( card ) ) {
            channel = "Mic";
        } else {
            card[i] = 0;
            channel = card + i + 1;
        }
        open_mixer();
        if (!handle) {
            fprintf( stderr, "mixer: Can't open mixer %s, volume unavailable.\n", card );
            return -1;
        }
        return 0;
}

Please help me to prevent memory leak after call strdup

user1840007
  • 615
  • 1
  • 10
  • 19
  • The key to avoiding memory leaks is to free allocated memory once you're done with it... – Oliver Charlesworth Aug 26 '13 at 19:33
  • static char *card=NULL, *channel=NULL; You can't assign to "Mic" to channel because it doesn't have any memory associated with it, just a pointer. You might be better off with char *channel[10] = {}; – Jiminion Aug 26 '13 at 19:38

1 Answers1

0

The function strdup allocates memory with malloc. If you want to avoid memory leak because of strdup you must free the return value of strdup (in your case the variable card) when you are no longer using the data.

Here is the part of the strdup man that states this :

char *strdup(const char *s);

The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

Zerkan
  • 51
  • 4