Fix segmentation errors in C ++

I am writing a cross-platform C ++ program for Windows and Unix. On the window side, the code will compile and not run without problems. On the Unix side, it will compile, however, when I try to run it, I get a segmentation error. My initial hunch is that the problem is with pointers.

What are good methodologies for troubleshooting segmentation error errors?

+60
c ++ debugging segmentation-fault
Sep 15 '10 at 15:09
source share
6 answers
  • Compile your application with -g , then you will have debugging symbols in the binary.

  • Use gdb to open the gdb console.

  • Use file and pass it the application binary in the console.

  • Use run and pass any arguments that your application needs to run.

  • Do something to cause a segmentation error.

  • Type bt in the gdb console to get a stack trace with a segmentation error.

+94
Sep 15 '10 at 15:12
source share
— -

Sometimes an accident in itself is not the real cause of the problem - it is possible that the memory was broken at an earlier stage, but it took time for corruption to show itself. Check out valgrind , which has a lot of checks for pointer problems (including checking the bounds of an array). He will tell you where the problem begins, and not just where the accident occurred.

+29
Sep 15 '10 at 15:16
source share

Before a problem arises, try to avoid it as much as possible:

  • Compile and run your code as often as you can. It will be easier to find the defective part.
  • Try to encapsulate low-level / error-prone routines so you rarely have to work directly with memory (pay attention to the modeling of your program)
  • Maintain a test suite. A review of what is currently working, what is no longer working, etc., will help you find out where the problem is ( boost test is a possible solution, I do not use it myself, but the documentation can help to understand what kind of information should be displayed )

Use appropriate debugging tools. On Unix:

  • GDB can tell you where you crashed and show you in what context.
  • Valgrind helps you discover many memory errors.
  • With GCC, you can also use mudflap. With GCC and Clang, you can use Address / Memory Sanitizer . It can detect some errors that Valgrind does not have, and the performance loss is less.

Finally, I would recommend the usual things. The more your program is readable, understandable and understandable, the easier it is to debug it.

+16
Sep 15 '10 at 15:53
source share

On Unix, you can use valgrind to find problems. It is free and powerful. If you prefer to do this yourself, you can overload the new ones and delete the statements to set up a configuration in which you have 1 byte with 0xDEADBEEF before and after each new object. Then trace what happens at each iteration. This may not catch everything (you are not even guaranteed to touch these bytes), but it worked for me in the past on the Windows platform.

+3
Sep 15 '10 at 15:12
source share

Yes, a problem with pointers. Most likely you are using one that is not initialized properly, but it is also possible that you messed up memory management with double releases or some of those.

To avoid uninitialized pointers as local variables, try declaring them as late as possible, preferably (and this is not always possible) when they can be initialized with a significant value. Convince yourself that they will be valuable before they are used by learning the code. If you have difficulty with this, initialize them with a null pointer constant (usually write as NULL or 0 ) and check them.

To avoid uninitialized pointers as element values, make sure they are properly initialized in the constructor and correctly handled in copy constructors and assignment operators. Do not rely on the init function to manage memory, although you can for a different initialization.

If your class does not need copy constructors or assignment operators, you can declare them as private member functions and never define them. This will cause a compiler error if they are used explicitly or implicitly.

Use smart pointers, if applicable. The big advantage here is that if you stick to them and use them sequentially, you can completely avoid the delete entry and nothing will be deleted twice.

Use C ++ strings and container classes whenever possible, instead of C-style strings and arrays. Consider using .at(i) rather than [i] , because this will force a border check. See if you can install your compiler or library to check boundaries on [i] , at least in debug mode. Segmentation errors can be caused by buffer overflows that write garbage over perfectly good pointers.

Performing these steps will significantly reduce the likelihood of segmentation failures and other memory problems. They will certainly not be able to fix everything, and why you should use valgrind from time to time when you have no problems, and valgrind and gdb when you do this.

+2
Sep 15 '10 at 17:28
source share

I do not know of any methodology used to correct such things. I don’t think it would be possible to come up with one of them for the problem itself: the behavior of your program is undefined (I don’t know of any cases where SEGFAULT was not called by some UB).

There are all kinds of “methodologies” to avoid a problem before it occurs. One important is RAII.

In addition, you just need to throw your best psychic energies into it.

0
Sep 15 '10 at 16:43
source share



All Articles