lundi 19 mars 2018

STL set is being corrupted after returning it resulting in SegFault

I'm using g++ -std=c++11 to compile this code with CMake, using option set (CMAKE_CXX_STANDARD 11).

I'm returning a set<unsigned int> by value in a class method. This set is declared and filled inside this method. I checked with gdb, and it was ok right before the returning statement:

(gdb) print accessed_states
$1 = std::set with 4 elements = {[0] = 0, [1] = 1, [2] = 2, [3] = 3}

However, right after returning, when receiving this set on another class method:

auto accessed_states = Bfs_();

The set object is corrupted:

(gdb) print accessed_states                         
$2 = std::set with 7142752 elements<error reading variable: Cannot access memory at address 0xc99000000d44e8d7>

And then, when accessing the content of the set object, I got a Segmentation Fault:

Thread 1 "basics" received signal SIGSEGV, Segmentation fault. 0x00007ffff7581903 in std::local_Rb_tree_increment (__x=0xc99000000d44e8c7) at ../../../../../libstdc++-v3/src/c++98/tree.cc:65

65 while (__x->_M_left != 0)

What surprised me here is the fact that g++ is using a c++98 compatible library instead of the c++11 version. My suspicious is that this is generating the error. Any ideas about what is happening here and how to solve it?

G++ version

g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

CMake version

cmake version 3.10.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Aucun commentaire:

Enregistrer un commentaire