0
如何避免双重释放或腐败(!prev
)如何避免双重释放或腐败
我有这些结构:
空设备,它始终工作
static struct mixer null_mixer = {
.set_device = null_set_device,
.close_device = null_close_device,
};
所有可用访问方法的列表。使用弱符号:NULL未链接。
static struct mixer *mixers[] = {
&alsa_mixer,
&oss_mixer,
&null_mixer
};
实际访问方法。
struct mixer *mixer = &null_mixer;
设置混频器设备和通道。尝试每个访问方法,直到一个成功。
void mixer_set_device(const char *devname)
{
int i;
mixer->close_device();
for (i = 0; i < sizeof(mixers)/sizeof(mixers[0]); i++) {
mixer = mixers[i];
if (!mixer)
continue;
if (mixer->set_device(devname) == 0)
break;
}
}
我收到以下错误,当用户尝试在GTK入门插入错误值搅拌机:
double free or corruption (!prev): 0x0000000002518c00 ***
我觉得这里是问题:
static void alsa_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, &alsa_min, &alsa_max);
if ((alsa_max - alsa_min) <= 0) {
goto error;
}
snd_mixer_selem_id_free(sid);
return;
error:
if (sid)
snd_mixer_selem_id_free(sid); /* THIS LINE */
if (handle) {
snd_mixer_close(handle);
handle = NULL;
}
return;
}
static int alsa_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 = "Line";
} else {
card[i] = 0;
channel = card + i + 1;
}
alsa_open_mixer();
if (!handle) {
return -1;
}
return 0;
}
struct mixer alsa_mixer = {
.set_device = alsa_set_device,
.close_device = alsa_close_device,
};
双重释放或腐败( !prev
):0x0000000002518c00 *
UPDATE:
指出有问题的
“混音器的错误值”是指* null_mixer *还是别的? – 2013-02-22 13:57:50
这个检查oss混音器例如/ dev/mixer:线或alsa混音器例如HW:0 /线。如果用户尝试输入hw:1并且卡不存在,请获取此错误。谢谢 – user1840007 2013-02-22 14:32:32
请告诉我们实际发生在哪一行。要么在调试器中运行,要么在valgrind中运行。确保你已经用'-g'构建。 – 2013-02-23 06:02:15