Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

Finding Segmentation Faults

This topic is 3828 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

Is there a (free) way to find segmentation faults? I've heard of Valgrind, but that seems Linux only (I'm looking for Windows). Thanks for the help!

Share this post


Link to post
Share on other sites
Advertisement
Well, you have several (free) options. These are to either use a debugger or put debugging statements in your program.

Using debugging statements is the easiest in terms of difficulty, but it is more time consuming. What this entails is going through your program and adding printf() or std::cout statments such as: std::cout << "Variable X is: " << x; If you put enough of these in your program, you might see something wrong with your variables which could be causing it.

Or, you can try to see which line of code is causing the seg fault. An example is this.

std::cout << "Before function foo()";
foo();
std::cout << "After function foo()";

If "Before function foo()" gets printed, but "After function foo()" doesn't, then you know the segfault is in function foo.

Another way of finding it (which I recommend) is to use a debugger. There is probably one in Visual Studio Express, or you can get MinGW, which includes the GDB debugger. You then will run your application inside GDB, and when it fails, it will show you where exactly the seg fault is coming from.

You will have to make sure to compile your code with debugging symbols enabled to use GDB, but this isn't really that hard at all. If you can't find how to do it in Visual Studio, MinGW also has the gcc compiler, and the syntax is below. Namely, you just need to use the -g flag.

To compile with debugging symbols:
gcc -g -o ExecutableThing.exe ExecutableSource.cpp

To use gdb type the following on a command line:
gdb Executable.exe
(now inside gdb)
run
***It will seg fault somewhere***
backtrace (this will display all the functions that are on the stack and you will be able to see which function caused the seg fault.)

Hope that helps! If you have any questions, let us know. Or, you could post some code if you still can't figure it out.

Share this post


Link to post
Share on other sites
I just looked at Valgrind, and since it's open source, you could just build it yourself if you really feel up to it. But for your needs, GDB seems like it is much more appropriate for finding your problem.

Share this post


Link to post
Share on other sites
MSVC will provide everything you need right out of the box. If you want a better tool you can use BoundsChecker by Compuware. It's probably one of the best out there and they have some really strong suites of applications.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!