I thought segmentation fault only causes crashes. But when I run my c++ program (on windows with mingw compiler) it hangs. But when I run it in gdb it says segmentation fault.
That should be much easier to solve than the reverse: If it seg faulted without the debugger but hangs with the debugger, that can be tricky to debug. Anything that seg faults with a debugger gives you a very easy way (get a backtrace after it seg faults) to get the direct cause of the seg fault, which usually (but not always) directly indicates the bug.
For your title question, you will likely discover the specific situation after you discover the cause of your seg fault. But in theory, it is possible but unlikely: In some environments some segfaults can be caught by try/catch and your code could be structured so catching such a fault puts the program in an infinite loop. More likely the same bug that causes a seg fault in the debugger causes a hang outside the debugger. Likely the debugger loads the program, its stack and/or its command line arguments at slightly different virtual addresses. Some random memory clobber might trigger a hang for one load location and seg fault for another. Alternately, some debuggers intentionally modify uninitialized memory, so that use of an uninitialized pointer is much more likely to seg fault, when it might cause other behavior (including hanging) without that interference by the debugger.
Related
I have a complicated application with lots of third-party libraries, dynamically loaded plugins. And something causes the app to crash (SIGSEGV) after main exits. The call stack points to unknown addresses so not only I can't debug, I don't even have an idea where the crash happens.
I tried to run the app with Valgrind - it shows leaks (some kilobytes) but I believe that they are false positives and/or I can't do anything about them because they are coming from the third-party.
My question: I believe that memory leaks can't cause a segmentation fault, at least I can't find out the possible scenario. But since I'm not sure I'd like to hear cases when a leak can break the program (assuming it's not a crazy leak when I'm simply out of memory).
No, memory leaks by themselves would not cause a segmentation fault. However, memory leaks usually indicate sloppy code, and in sloppy code other issues, which would cause a segmentation fault, are likely to be present.
No, a segmentation fault in itself is not that much more than trying to access a piece of memory that you are not allowed access to. A memory leak on the other hand is when you allocate some memory and later on 'forget' the location of the piece of memory. The data stored is still there, but it cannot be accessed anymore from that program instance.
Both errors/faults almost always occur because of sloppy coding practices. So it might be possible that the same sloppy coding that causes a memory leak is also responsible for segmentation faults.
I'm using GTest for unittesting my project.
One of the functions I want to test terminates with a Segmentation Fault, which is is not the correct behavior of this function.
I want gtest to report this error, like every other error, but it doesnt. It doesnt run other test cases and it doenst print a report at the end.
It there a way to get a correct report from gtest for this kind of behavior?
You didn't link any code example, so the best answer I can give you is very general.
Your problem is either:
Your function is incorrect and is segmentation faulting (edge cases or main body)
You wrote your test badly, and it (the test itself) is segmentation faulting
Seg Faulting happens when your program/test tries to do something it isn't allowed to do (i.e.. you try to dereference a null ptr, access memory that isn't yours, etc..). Google Test is not able to handle the signal SIGSEGV (nobody is), so your program will crash. Seg Faults are the operating systems way of telling you that you did something illegal and they instantly terminate your program. You need to fix the segmentation fault in order to get results. I would recommend doing the following things:
Learn how to use GDB, and run your program in GDB to figure out where it is segmentation faulting
Use print statements to figure out where your program is segmentation faulting.
How to know if it is a memory problem when a program crashes while running whith a display "the program has stopped working"
My program crashes a lot and I cannot find any memory problems.
What other than memory problems crashes a program?
usually when u don't dis-allocate used memory by pointers, programs tend to crash at the end.
Not having enough memory space crashes Cuda programs.
Other includes using out of bounds array...both in host and device in case of cuda.
Try and use exception handlers as far as possible so that you can trace your error.
in your case, since it stops in runtime...try and use a debugger. you will find the mistake. there are debuggers for cuda as well. you need to select one thread and follow the trace.
Make sure you dont use out of bounds arrays in threads as well.
+
I am running a C/C++ program in linux servers to serve videos. The program's(say named Plugin) core functionality is to convert videos and we fork a separate Plugin process for each video request. But I am having a weird problem for which sometimes server load average gets unexpectedly high. What I see from top command at this stage is that there are some processes which are running for long time and taking some huge CPU's.
When I debug this running program with gdb and backtrace stack,what I found is the corrupt stack: "Previous frame inner to this frame (corrupt stack?)". I have searched the net and found that this occurs if the program gets segmentation fault.
But what I know if the program gets segmentation fault, the program should crash and exit at that point. But surprisingly the program still running after segmentation fault.
What can be the causes of this? I know there must be some big problems in the program but I just can't understand from where to start fixing the problem...It would be great if any of you can show me some lights...
Thanks in advance
Attaching the debugger changes the behavior of the process so you won't get reliable investigation results most probably. Corrupted stack message from the debugger can mean that the particular debugger does not understand text info from the binary.
I would recommend running pstack several time subsequently on the problematic (this is known as "Monte Carlo performance profiling") and also attach strace or truss to the problematic and check what system calls is the process doing when consuming CPU.
Run your program under Valgrind and fix any invalid memory writes that it finds.
Certain optimisations, such as frame pointer omission, can make it harder for the debugger to understand the stack.
If you have the code, compile the program in debug and run Valgrind on it.
If you don't have the code, contact the author/provider of the program.
The corrupt stack message simply means the code is doing something weird with the memory. It does not mean the program has a segmentation fault. Also, the program can still run if it choose to handle the SIGSEGV signal.
If by forking you mean that you have some process which spawn and run other smaller processes, just monitor for such spikes and restart the process. This assumes that you have no access to the fix the program.
There could be some interesting manipulation of the stack taking place through assembly code manipulation, such as true tail-recursion optimization, self-modifying code, non-returning functions, etc. that may cause the debugger to be incapable of properly back-tracing the stack and causing it to trigger a corrupted stack error, but that doesn't necessarily mean that memory is corrupted ... but definitely something non-traditional is happening under the hood.
I have some code, that I'm currently porting from OS X to Linux (console tool).
Somewhere in this code, I get a segmentation fault. The problem is, that if I run the program without GDB, I clearly see the segmentation fault, and the program is killed. But when I'm running GDB it just halts, and GDB never returns to the prompt. So I'm not really able to examine what's going on.
C++ is the code. Compiled with the -g option in g++.
Btw. Pretty new to GDB, so excuse me if this is obvious.
Any ideas?
Thanks in advance.
Trenskow
gdb will suspend your program when the seg fault signal is received
type where to see the stack trace and start inspecting what's going on from there.
Also consider enabling core dumps, that way you can load the core dump in GDB and investigate what is going on
you can then load the core dump like this
> gdb your_program the_core_dump
The behaviour you describe is not typical - I suspect the stack may have been trashed.
Try sending various signals directly via the 'kill' command.
Might be worth you running a test program in gdb with an abort() in it so that you can learn what the expected behaviour is for gdb.
I've seen this before when my stack was too large. Try moving stack variables onto the heap (make them globals), recompile, and see if you still get the error.