错误条件块中的代码可能出现分段错误吗?

问题描述 投票:0回答:1

我使用ALSA API与linux中的USB音频编解码器连接。我的代码检查-r-p标志,并分别记录或播放音频:

 int main(int argc, char** argv){
   if(argc > 1){
     if(strcmp("-p", argv[1]) == 0){
       // Create data buffer, choose sampling rate
       char* playback_data;
       unsigned int sampling_rate = 44100;


       // Declare handle and params structure
       snd_pcm_t* handle;
       snd_pcm_hw_params_t* params;

       // Initiate and configure sound device
       init_playback_device(&handle, &params);
       config_playback_device(handle, params, sampling_rate);

       // Playback
       printf("playing...\n");
       playback(handle, params, playback_data, "test.raw", 3);

       // Close
       snd_pcm_hw_params_free(params);
       snd_pcm_close(handle);
       free(playback_data);

     } else if(strcmp("-r", argv[1]) == 0){
       // Create data buffer, choose sampling rate
       char* record_data;
       unsigned int sampling_rate = 44100;

       // Declare handle and params structure
       snd_pcm_t* handle;
       snd_pcm_hw_params_t* params;

       // Initiate and configure sound device
       init_recording_device(&handle, &params);
       config_recording_device(handle, params, sampling_rate);

       // Record
       printf("recording...\n");
       record(handle, params, record_data, "test.raw", 3);

       // Close
       snd_pcm_hw_params_free(params);
       snd_pcm_close(handle);
       free(record_data);
     }
   }
   return 0; 
 }

每当我运行./audio -r时,我都会遇到分段错误。奇怪的是,当我在第一个if语句(-p标志)中注释掉代码时,我没有得到分段错误。我不明白为什么评论未首先运行的代码(由于错误的条件)会产生段错误。使用valgrind会显示以下问题:

==22959== Conditional jump or move depends on uninitialised value(s)
==22959==    at 0x4848BAC: free (vg_replace_malloc.c:530)
==22959==    by 0x10DA7: main (audio.c:59)
==22959==  Uninitialised value was created by a stack allocation
==22959==    at 0x10C5C: main (audio.c:14)
==22959==
==22959== Invalid free() / delete / delete[] / realloc()
==22959==    at 0x4848BFC: free (vg_replace_malloc.c:530)
==22959==    by 0x10DA7: main (audio.c:59)
==22959==  Address 0x10b2c is in the Text segment of /home/pi/audio/audio
==22959==    at 0x10B2C: ??? (in /home/pi/audio/audio)

它不喜欢free(record_data)线,因为它认为record_data是未初始化的,但是在我的record函数中,我会照顾这个:

 void record(snd_pcm_t* handle, snd_pcm_hw_params_t* params, char* data_buffer, char file[], int duration){
   // Open file
   FILE* audio_file;
   audio_file = fopen(file, "w+");

   data_buffer = (char*) malloc(NUM_FRAMES * 4);

   int i;
   for(i = 0; i < (duration*10); i++){
     snd_pcm_readi(handle, data_buffer, NUM_FRAMES);
     fwrite(data_buffer, sizeof(char), NUM_FRAMES * 4 , audio_file);
   }

   fclose(audio_file);
 }

指向record_data的指针作为参数data_buffer传递。任何人都知道出了什么问题?

编辑:

解决方法是传递record_data的指针,因为playback函数发出了一个指向局部变量的指针:

 void record(snd_pcm_t* handle, snd_pcm_hw_params_t* params, char** data_buffer, char file[], int duration){
   // Open file
   FILE* audio_file;
   audio_file = fopen(file, "w+");
   *data_buffer = (char*) malloc(NUM_FRAMES * 4);
   int i;
   for(i = 0; i < (duration*10); i++){
      snd_pcm_readi(handle, *data_buffer, NUM_FRAMES);
     fwrite(*data_buffer, sizeof(char), NUM_FRAMES * 4 , audio_file);
   }
   fclose(audio_file);
 }

我仍然很好奇为什么在第一个条件块中注释掉代码会删除错误。仍然不确定为什么会这样。

c memory segmentation-fault
1个回答
1
投票

首先,Valgrind是对的。您正在使用单位指针。在写入该位置之前,您需要先分配内存。在函数内部分配内存时,只会覆盖指针的本地值,但不要将新指针返回到周围的代码中。

比较record_data指向函数调用之前,之内和之后的位置。

考虑在记录函数内移动free调用,或返回更新的数据指针,或通过引用传递。这些问题中的任何一个都应该解决大部分(如果不是全部)问题。

© www.soinside.com 2019 - 2024. All rights reserved.