Home > Net >  __static_initialization_and_destruction_0 seg fault
__static_initialization_and_destruction_0 seg fault

Time:10-02

I was developing a C program and everything was working fine. Then, while I was programming, I ran make and ran my program like usual. But during the execution of it all, my computer crashed and shut itself off. I reopened my computer and ran make again but this time it gave me a bunch of errors.

Everything seemed off, like my whole computer is corrupted. But everything was working as intended in my operating system except the stuff related to my program. I've tried making a new c project, it works just fine. I've tried deleting the project and re-compiling it from github to no avail.

I managed to compile the program in the end but now it gives me a seg fault. The first thing my program does is to print out Starting... to the screen, but this segfault occurs without ever printing that, so it led me to believe that this error is linker related. (Even when the make command was failing, before I fixed it, it told me there was a linker error)

Here is what valgrind says:

turgut@turgut-N56VZ:~/Desktop/CppProjects/videoo-render$ valgrind bin/Renderer
==7521== Memcheck, a memory error detector
==7521== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7521== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==7521== Command: bin/Renderer
==7521== 
==7521== Invalid read of size 1
==7521==    at 0x484FBD4: strcmp (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7521==    by 0x121377: __static_initialization_and_destruction_0 (OpenGLRenderer.cpp:111)
==7521==    by 0x121377: _GLOBAL__sub_I__ZN6OpenGL7Texture5max_zE (OpenGLRenderer.cpp:197)
==7521==    by 0x659FEBA: call_init (libc-start.c:145)
==7521==    by 0x659FEBA: __libc_start_main@@GLIBC_2.34 (libc-start.c:379)
==7521==    by 0x1216A4: (below main) (in /home/turgut/Desktop/CppProjects/videoo-render/bin/Renderer)
==7521==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==7521== 
==7521== 
==7521== Process terminating with default action of signal 11 (SIGSEGV)
==7521==  Access not within mapped region at address 0x0
==7521==    at 0x484FBD4: strcmp (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==7521==    by 0x121377: __static_initialization_and_destruction_0 (OpenGLRenderer.cpp:111)
==7521==    by 0x121377: _GLOBAL__sub_I__ZN6OpenGL7Texture5max_zE (OpenGLRenderer.cpp:197)
==7521==    by 0x659FEBA: call_init (libc-start.c:145)
==7521==    by 0x659FEBA: __libc_start_main@@GLIBC_2.34 (libc-start.c:379)
==7521==    by 0x1216A4: (below main) (in /home/turgut/Desktop/CppProjects/videoo-render/bin/Renderer)
==7521==  If you believe this happened as a result of a stack
==7521==  overflow in your program's main thread (unlikely but
==7521==  possible), you can try to increase the size of the
==7521==  main thread stack using the --main-stacksize= flag.
==7521==  The main thread stack size used in this run was 8388608.
==7521== 
==7521== HEAP SUMMARY:
==7521==     in use at exit: 72,741 bytes in 3 blocks
==7521==   total heap usage: 3 allocs, 0 frees, 72,741 bytes allocated
==7521== 
==7521== LEAK SUMMARY:
==7521==    definitely lost: 0 bytes in 0 blocks
==7521==    indirectly lost: 0 bytes in 0 blocks
==7521==      possibly lost: 0 bytes in 0 blocks
==7521==    still reachable: 72,741 bytes in 3 blocks
==7521==         suppressed: 0 bytes in 0 blocks
==7521== Rerun with --leak-check=full to see details of leaked memory
==7521== 
==7521== For lists of detected and suppressed errors, rerun with: -s
==7521== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
turgut@turgut-N56VZ:~/Desktop/Cpp

OpenGLRenderer.cpp:197 is just the end of the file and here is what's writeen in OpenGLRenderer.cpp:111:

static bool __debug = strcmp(getenv("DEBUG"), "true") == 0;

It says that there is an error with strcmp but I've tried using that function on a different project and it worked just fine.

What could be the reason for this? I'm on ubuntu 22.04, gcc verison 11.2.0.

CodePudding user response:

this segfault occurs without ever printing that, so it led me to believe that this error is linker related.

The linker is not involved in your program running, so it can't be "linker related".

There is a dynamic loader (if your program uses shared libraries), so perhaps that's what you meant.

In any case, the crash is happening because OpenGLRenderer.cpp:111 (probably in libGL.so) is calling strcmp() with one of the arguments being NULL (which is not a valid thing to do). This does happen before main.

This line:

static bool __debug = strcmp(getenv("DEBUG"), "true") == 0;

is buggy: it will crash when DEBUG is not set in the environment (getenv("DEBUG") will return NULL in that case).

As a workaround, you can run export DEBUG=off, before running your program and the crash will go away.

It's unclear whether you inserted this line into OpenGLRenderer.cpp yourself or whether it was already present, but it's buggy either way.

P.S. A correct way to initialize __debug could be:

static const char *debug_str = getenv("DEBUG");
static const bool debug = strcmp(debug_str == NULL ? "off" : debug_str, "true") == 0;

P.P.S. Avoid using identifiers prefixed with __ (such as __debug) -- they are reserved.

  • Related