【问题标题】:C segmentation faults due to fopen(). How to trace and what to look for?由于 fopen() 导致的 C 分段错误。如何追踪和寻找什么?
【发布时间】:2010-09-11 01:10:01
【问题描述】:

(这是在 ffmpeg-devel 列表中询问的,但算得上离题,所以在这里发布)。

ffmpeg.c 加载多个 .c,它们使用 log.c 的 av_log -> av_log_default_callback 函数,使用 fputs;

void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
{
...
snprintf(line, sizeof(line), "[%s @ %p] ", (*parent)->item_name(parent), parent);
... call to colored_fputs

画面输出:

static void colored_fputs(int level, const char *str){

...
fputs(str, stderr);

// this causes sigsegv just by fopen()
FILE * xFile;
xFile = fopen('yarr', 'w');
//fputs(str, xFile);fclose(xFile); // compile me. BOOM!
av_free(xFile); // last idea that came, using local free() version to avoid re-creatio

每次将 fopen 放入代码中时,都会给出原因不明的分段错误。为什么这种事情会发生在这里?可能是因为阻塞了主 I/O?

在这种情况下应该调查哪些一般的“阻碍因素”? Pthreads(在某处涉及代码)?

【问题讨论】:

  • 您的意思是在fopen 调用中使用双引号而不是单引号吗?这段代码编译了吗?

标签: c segmentation-fault


【解决方案1】:

fopen 接受字符串作为参数,你给它的是 char 字面量

 xFile = fopen('yarr', 'w');

应该是

xFile = fopen("yarr", "w");
if(xFile == NULL) {
  perror("fopen failed");
  return;
}

编译器应该对此发出警告,因此请确保您已打开警告标志(请记住阅读并修复它们)

【讨论】:

  • 谢谢,太简单了,差点忘了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 2018-12-09
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多