我不会将每一行都限制在char **结果中。我做了以下事情:
static char **alsa_get_device(void)
{
int o;
char **result;
o = 0;
for (elem = snd_mixer_first_elem(handle); elem; elem = snd_mixer_elem_next(elem)) {
if (!snd_mixer_selem_is_active(elem))
continue;
if (!snd_mixer_selem_has_playback_volume(elem))
continue;
result[o] = strdup(snd_mixer_selem_get_name(elem));
o++;
result[o] = NULL;
}
return result;
}
并使函数返回GList:
GList *get_mixer_list(void)
{
int i;
char **array, *dev;
GList *result = NULL;
array = alsa_get_device();
if (!array)
return NULL;
i = 0;
dev = array[i];
while (dev) {
char *text = g_strdup(dev);
result = g_list_append(result, text);
free(dev);
dev = array[++i];
}
free(array);
return result;
}
但是当我使用时崩溃:result [o] = strdup(snd_mixer_selem_get_name(elem));
答案 0 :(得分:0)
以下是你的代码中出错的地方:
result
未使用未初始化free(array)
malloc()
在此细分中:
char *text = g_strdup(dev);
result = g_list_append(result, text);
free(dev);
dev = array[++i];
您应该将free(dev)
替换为free(text)