Hi,
The attached code produces a segfault (on Ubuntu 18.04 with GCC 7.3) but
not if I uncomment g() in main(). Any ideas why?
Best regards,
Marcel
$ g++ -g A.cpp ; valgrind ./a.out
==28698== Memcheck, a memory error detector
==28698== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==28698== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==28698== Command: ./a.out
==28698==
==28698== Invalid write of size 4
==28698== at 0x10874B: B::f() (A.cpp:7)
==28698== by 0x108788: void f<int>() (A.cpp:15)
==28698== by 0x1086D6: main (A.cpp:26)
==28698== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==28698==
==28698==
==28698== Process terminating with default action of signal 11 (SIGSEGV)
==28698== Access not within mapped region at address 0x0
==28698== at 0x10874B: B::f() (A.cpp:7)
==28698== by 0x108788: void f<int>() (A.cpp:15)
==28698== by 0x1086D6: main (A.cpp:26)
==28698== If you believe this happened as a result of a stack
==28698== overflow in your program's main thread (unlikely but
==28698== possible), you can try to increase the size of the
==28698== main thread stack using the --main-stacksize= flag.
==28698== The main thread stack size used in this run was 8388608.
==28698==
==28698== HEAP SUMMARY:
==28698== in use at exit: 0 bytes in 0 blocks
==28698== total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated
==28698==
==28698== All heap blocks were freed -- no leaks are possible
==28698==
==28698== For counts of detected and suppressed errors, rerun with: -v
==28698== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
class B
{
int* x;
public:
static thread_local B tmp;
B() { x = new int(); }
void f() { *x = 0; }
};
thread_local B B::tmp;
template<class T>
void f()
{
B::tmp.f();
}
void g()
{
B::tmp.f();
}
int main()
{
//g();
f<int>();
}