kagali-san
kagali-san

Reputation: 3082

C segmentation faults due to fopen(). How to trace and what to look for?

(this was asked on ffmpeg-devel list, but counted way offtopic, so posting it here).

ffmpeg.c loads multiple .c's, that are using log.c's av_log -> av_log_default_callback function, that uses 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

Screen output:

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

Each time, when fopen is put into code, it gives a segmentation fault of unknown reason. Why this kind of thing may happen here? Maybe due to blocking main I/O?

What are general 'blockers' that should be investigated in such a situation? Pthreads (involved in code somewhere)?

Upvotes: 0

Views: 2588

Answers (1)

nos
nos

Reputation: 229184

fopen takes strings as arguments, you're giving it char literals

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

Should be

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

The compiler should have warned about this, so make sure you've turned warning flags on (remeber to read them and fix them)

Upvotes: 4

Related Questions