erthalion
erthalion

Reputation: 3244

Segmentation fault at global variable initialization in shared library

I have a shared library that contains global variable


    int ***carg;

This variable is initialized at function


    __attribute__((constructor)) void init(void) {
        carg = generator->get_carg();
    }

where


    int*** get_carg();

just returns preinitialized carg array.

If this library is loading there is segmentation fault

Program received signal SIGSEGV, Segmentation fault. 0x00007fffebe05fea in init () at /path/to/project/main.cpp:814

In this frame


    (gdb) print carg
    $1 = (int ***) 0x0
    (gdb) call generator->get_carg()
    $2 = (int ***) 0xf12410

Why this assignment (one pointer is assigned to other pointer) is the cause of segmentation fault? And how can I initialize this variable?

UPD: I still don't know answer, but when carg variable was renamed, there is no errors. I thought that there is one more variable with this name, but that's not so.

Upvotes: 1

Views: 1536

Answers (1)

sth
sth

Reputation: 229844

Probably the generator variable is not initialized yet and doesn't point to a valid object.

Calling get_cargs() then results in a segmentation fault.

Upvotes: 1

Related Questions