Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help trying to debug a weird segmentation fault

Tags:

c++

solaris

Background

While moving to a newer version of CC compiler, a segfault manifested in a module that used to work.

Observations so far

  1. From the core file I could learn in what function the segfault originated. When I observed the function I could not find anything suspicious.

  2. First major problem was that the segfault reproduced only when compiling in "release" (optimizations turned on), and didn't reproduce on "debug". Also, the segfault doesn't reproduce on g++.

  3. Now I started to use printing, and a bigger problem arose- when adding cout/printf (to binary search the segfault line/print the value of pointers) to certain lines in the code, the segfault didn't reproduce. Moreover, I added a cout in a certain line in the code that maintained the segfault, which supposedly means that the segfault happens before that line. Commenting lines after that line made the segfault go away.

To me, this screams memory corruption (specifically of the stack), but I have no idea on how to advance on this without looking at the generated assembly.

Any ideas? thanks in advance.

I'm working on SunOS_5.10_Studio_12_5.12_64, CC version "Sun C++ 5.12 SunOS_sparc 2011/11/16"

More details in response to comments

  1. The code is single-threaded.
  2. valgrind is not available on Solaris so it's not relevant.
like image 673
infokiller Avatar asked Jul 25 '12 15:07

infokiller


People also ask

Why is my code giving segmentation fault?

A common run-time error for C programs by beginners is a "segmentation violation" or "segmentation fault." When you run your program and the system reports a "segmentation violation," it means your program has attempted to access an area of memory that it is not allowed to access.

How can segmentation fault be avoided?

Regarding Best practices to avoid segmentation faults, testing the code with tools like Valgrind/Efence helps in catching memory over runs. Besides using tools, writing and organising code carefully helps to great extent. Hope this helps.

Does segmentation fault mean memory leak?

Most memory errors which aren't memory leaks end up resulting in a segmentation fault. A segmentation fault is raised when the operating system realizes that your program is trying to access memory that it shouldn't have access to.


1 Answers

You should use a memory debugger/profiler like valgrind. It will quickly tell you the location of corruption. On Solaris you can try libumem.

like image 102
perreal Avatar answered Sep 20 '22 16:09

perreal