0

I have a fairly complex C++ code that generates a segmentation fault as soon as it loads. The first statement in main() never gets executed. I think the problem lies in a static library libWhatever.a because the segmentation fault only occurs if I try to instantiate an object from that library. How would a smart person debug this problem?

EDIT #1: The code that instantiates the object never executes. In fact I put it inside an if-statement with a clause that I know is impossible (but the compiler does not know). That is why I think the code is seg-faulting at load-time.

JB_User
  • 3,117
  • 7
  • 31
  • 51
  • 1
    `if I try to instantiate an object` You should be able to set a breakpoint on that and catch it in the debugger, even if it happens before `main`, presumably during construction of some global object. – dxiv Feb 15 '21 at 22:38
  • 2
    If you're not sure where the erroring code is, it sounds like you may need to learn how to [display a stack trace](https://stackoverflow.com/q/691719/2602718) when your code breaks. – scohe001 Feb 15 '21 at 22:39
  • Do you have global variables that depend on each other? Maybe you have a case of static initialization order fiasco – drescherjm Feb 15 '21 at 22:39
  • show us minimum requirements –  Feb 15 '21 at 22:43
  • 1
    I don't know if I'm a smart person but I would type `gdb `, then the `R`un command, and wait until gdb interrupts the segfault and then see what's up. – Sam Varshavchik Feb 15 '21 at 22:49
  • Excellent advice. I will try all those things (unless I solve it). – JB_User Feb 15 '21 at 22:55
  • If no statement in `main()` is executed then it is some constructor of a global variable. So the next thing is to identify which global variable. Then understand why it is failing (probably because something is not initialized correctly). – Martin York Feb 15 '21 at 23:20

0 Answers0