Jump to content
  • Advertisement
Sign in to follow this  
walkingcarcass

Catch a segfault

This topic is 4381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. When programming under Linux with C++ I have trouble dealing with segfaults. Normal try and catch statements don't seem to work. Apparently Linux doesn't use SEH and the best advice I can find is to use "resource aquisition is initialisation" but this doesn't work either. The test program
#include <iostream>

class A {
        public:
                ~A(){
                        std::cerr << "Closing.\n";
                }
};

void a();

void b(){ a(); }

void a(){ b(); }

int main(){
        A tmp;

        a();
}

never calls the destructor for "tmp". So what's the trick?

Share this post


Link to post
Share on other sites
Advertisement
There isn't :)

Segfaults are faults when your program makes a critical error that it can't recover from. Exceptions are explicitly thrown, therefore you can catch them. Segfaults however just crash your program.

In your case it would probably cause a callstack overflow. The only thing you can do to prevent this is to don't write stupid code like that ;) :)

It is one of the drawbacks of C / C++, you will have to deal with it. One way to debug it is to output markers (printf("MARKER 1")) and try to locate the piece of code that creates the segfault, and isolate it and see what goes wrong
Usually it is some sort of out of bound or overflow error;

Greetings...

Share this post


Link to post
Share on other sites
The segfault above is from exhausting the stack. With exceptions the stack unwinds. This is only critical if the topmost exception handler itself exhausts the stack.

<tangent>Incidentally, anyone know why the above code doesn't start thrashing the swap file?</tangent>

Share this post


Link to post
Share on other sites
<tangent> I would guess because your program is allocated a fixed amount of stack space at start up, but I could be wrong </tangent>.

Share this post


Link to post
Share on other sites
Quote:
Original post by walkingcarcass
<tangent>Incidentally, anyone know why the above code doesn't start thrashing the swap file?</tangent>


Why would it thrash your swap file? The only memory you're using is allocated on the stack, and that (most likely) should never be in the swap file, it would be too slow for execution to write to disk for every push/pop of the program stack.

Share this post


Link to post
Share on other sites
Since it's used a lot, the stack should surely stay resident in memory and have good locality, so it shouldn't slow to a crawl. The bottom of the stack should get swapped out is what I mean. I'd rather have a performance hit than a segfault. How is it worse than swapping out parts of the heap?

Share this post


Link to post
Share on other sites
The stack has a fixed size. If you have a huge stack, you have a problem in your code, like an infinite recursion. If you have a huge heap, on the other hand, that could be something as sinister as an infinite loop of news, or as benign as a movie file.

Therefore, if you run out of stack space, it's better to crash and let the developer know there's a problem, rather than silently enlarge the stack and slow to a crawl.

Share this post


Link to post
Share on other sites
Quote:
Original post by walkingcarcass
Hello. When programming under Linux with C++ I have trouble dealing with segfaults. Normal try and catch statements don't seem to work. Apparently Linux doesn't use SEH [...]


IIRC, seg-fault in linux is delivered by signal, not by exception.

Quote:
Original post by walkingcarcass
The test program*** Source Snippet Removed ***never calls the destructor for "tmp".


Default signal handlers are implemented in C, and they are mostly just exit()-ing, with no regard to RAII whatsoever. Unfortunately, I am not a linux expert, so you'd have to dig a little deeper on this by yourself.

Share this post


Link to post
Share on other sites
Quote:
IIRC, seg-fault in linux is delivered by signal, not by exception.

That's correct. Catching the SIGSEGV signal is the *nix equivalent of catching the SEH exception. For information on doing that, look here.

Share this post


Link to post
Share on other sites
Quote:
Original post by walkingcarcass


<tangent>Incidentally, anyone know why the above code doesn't start thrashing the swap file?</tangent>


Under linux the stack segment is 256 mb by default, however you only use 2 mb as your stacksize which is fixed as already mentioned

In general the maximum segment size is 256 mb under linux which means you shouldn t be able to allocate more than 256 mb with the new operator or malloc

To get a larger chunk of memory you either have to recompile you kernel with adjusted header files or you memory map (man mmap) a file.

As for segfault detection there are tools like valgrind and efence
two powerful tools.

At university our projects have to compile fine with both valgrind and efence otherwise you get zero scores ^^

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!