What could be the possible reasons for segmentation fault?

Possible reasons for segmentation fault on function exit

  • What possible reasons can you think of for a segmentation fault on exiting a function in C++? What I mean by that is, that I have a reproducible segmentation fault in a C++ program, and when I investigate using GDB is says Program received signal SIGSEGV, Segmentation fault. FooBar (bla=...) at foo.cpp:59 59 } where the indicated line contains the closing bracket of my function.

  • Answer:

    There could be many reasons of this. Run program under Valgrind and most likely it will tell you exact reason or at least will help to investigate and narrow down the problem.

dcn at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

It's quite likely a http://en.wikipedia.org/wiki/Buffer_overrun on some buffer located on your stack. This overwrites the return address, so when your code tries to return to the previous stack frame, it instead jumps to some random address which is more likely than not non-executable, so you get a segmentation fault. But without seeing some more code or more information about the crash, it's impossible to say what the exact cause is.

Adam Rosenfield

My first guess is the destructor of a class is freeing an invalid pointer.

Rob K

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.