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

时间:2018-03-03 19:40:16

标签: c memory segmentation-fault

我使用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);
 }

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

1 个答案:

答案 0 :(得分:1)

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

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

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