• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1276
  • Last Modified:

What can cause Segmentation fault inside fclose@@GLIBC_2.1 () from /lib/tls/libc.so.6?


I'm running a program in Linux. This program reads a file, execute some commands based on the content of the file. After completing all the commands, the program closes the file. The program crahses (Segmentation Fault) when it closes the file. I used gdb stack trace (bt) to track where the Segmentation fault occurred and found that it occurs inside the function fclose@@GLIBC_2.1 () from /lib/tls/libc.so.6.

Here is the section of code that  closes the file:

      if (m_fptr)

Here is the logs:
Program received signal SIGSEGV, Segmentation fault.
0xb7386333 in fclose@@GLIBC_2.1 () from /lib/tls/libc.so.6

If you've any idea why the program crashes inside fclose@@GLIBC_2.1 () from /lib/tls/libc.so.6, please help.
Any suggestion would be appreciated.



2 Solutions
My guess would be that m_fptr has been trashed after the last prior use and befor the if() statement.
We cannot deduce the error from these 4 lines. It is very likely that your program has caused undefined behaviour somewhere prior. Can you post the code?
There are 4 common programming errors that would result in this:

1) m_fptr was not initialized to NULL, and some code path gets
to the call to fclose() without ever giving m_fptr a value.

2) the file has already been closed, but m_fptr was not reset to NULL.

3) you managed to overwrite the m_fptr variable, trashing the FILE pointer.

4) you managed to trash the FILE structure pointed to by m_fptr.

rfr1tzAuthor Commented:
Hi all,

Thanks  so much for your help.
Exactly as jlevie and brettmjohnson pointed out: the file has been closed/trahsed but m_fptr was not set to NULL.
I got it right now.

Thanks again,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now