Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 974
dcn Avatar asked Dec 22 '22 10:12

dcn


2 Answers

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.

like image 139
ks1322 Avatar answered Dec 23 '22 22:12

ks1322


It's quite likely a 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.

like image 42
Adam Rosenfield Avatar answered Dec 23 '22 23:12

Adam Rosenfield