我想用gtk_entry以「/ dev/mixer:line」或「/ dev/mixer:cd」的形式設置混音器設備。在調用strdup()之前指定值的空閒()字符strdup()
用戶必須以該格式混合器設備設置到條目:
/dev/mixer:line
or:
/dev/mixer:cd
爲此,我編寫代碼來設置混頻器和具有的strdup()函數一樣的困境。 在調用strdup()之前釋放帶賦值的char()字符是錯誤的?
char *mixer_device = "/dev/mixer";
int mixer_channel = SOUND_MIXER_LINE;
int fd = -1;
int get_volume(void)
{
int v, cmd, devs;
int curvol = 0;
if(fd < 0) fd = open(mixer_device, O_RDONLY);
if(fd != -1) {
ioctl(fd, SOUND_MIXER_READ_DEVMASK, &devs);
if(devs & mixer_dev_mask) {
cmd = MIXER_READ(mixer_channel);
} else {
return curvol;
}
ioctl(fd, cmd, &v);
curvol = (v & 0xFF00) >> 8;
}
return curvol;
}
char *core_devnames[] = SOUND_DEVICE_NAMES;
int set_device(const char *devname)
{
const char *channame;
int i;
/* if (mixer_device) free (mixer_device) <-- It is wrong ??? */
mixer_device = strdup(devname);
if(!mixer_device) return -1;
i = strcspn(mixer_device, ":");
if(i == strlen(mixer_device)) {
channame = "line";
} else {
mixer_device[ i ] = 0;
channame = mixer_device + i + 1;
}
fd = open(mixer_device, O_RDONLY);
if(fd == 0) {
fprintf(stderr, "mixer: Can't open device %s, "
"mixer volume and mute unavailable.\n", mixer_device);
return -1;
}
return 0;
}
這是錯誤的與調用的strdup()
調用'free'動態分配的內存是不確定的行爲。你在這裏設置初始值爲一個字符串文字:'char * mixer_device =「/ dev/mixer」;' –