Problems using _GLIBCXX_DEBUG with yaml-cpp

80 Views Asked by At

I find it convenient to use the flag _GLIBCXX_DEBUG when compiling c++ with gcc in debug builds since they enable range checking for std::vector and the like. However, when I use yaml-cpp to parse yaml files, I get a strange error when this flag is enabled. It works as expected without the flag.

I constructed an example as simple as possible to demonstrate the issue. I have the following main.cpp file:

#include <iostream>
#include <yaml-cpp/yaml.h>

int main()
{
    YAML::Node node = YAML::LoadFile("test.yml");
    std::cout << "value of x = " << node["x"] << "\n";
}

and this super simple yaml file called test.yml:

x: 1

From here I first built and ran it like this

g++ -o main main.cpp -lyaml-cpp -g 
./main

and got the expected output:

value of x = 1

If I then added the _GLIBCXX_DEBUG flag, i.e compiled and ran it like this:

g++ -o main main.cpp -lyaml-cpp -g -D_GLIBCXX_DEBUG
./main

I get the ouput:

Segmentation fault (core dumped)

If I run it in a debugger and view the call stack I get a bit more insight. It is this access node["x"] that causes the segfault, and the error occurs in this member function in the file impl.h:

template <typename Key>
inline Node Node::operator[](const Key& key) {
  EnsureNodeExists();
  detail::node& value = m_pNode->get(key, m_pMemory);
  return Node(value, m_pMemory);
}

I'm leaning towards that this is a bug in the yaml-cpp library, but perhaps it's just me who is using it wrongly.. I never know. I tried to remedy the problem by using skipping the _GLIBCXX_DEBUG option for the source files that uses yaml-cpp, but this caused some other issues. Unless I figure this out I guess I just have to make without the _GLIBCXX_DEBUG flag :(

1

There are 1 best solutions below

0
ander On

I Learned that this issue was caused by the fact that yaml-cpp was not installed with the _GLIBCXX_DEBUG flag enabled. This info is actually in the readme on github https://github.com/jbeder/yaml-cpp

I ended up installing two versions so that I can use it with and without _GLIBCXX_DEBUG.

These were installed the following way: I followed the installation procedure given here: https://github.com/jbeder/yaml-cpp, but I created two build diretories, build_debug and build_default. For the debug I used the following cmake command: cmake -DCMAKE_CXX_FLAGS="-D_GLIBCXX_DEBUG -g" -DCMAKE_INSTALL_PREFIX=/path/to/install_debug, the first option specifying that I want to build with -g and -D_GLIBCXX_DEBUG flags and the latter option specifying a non-standard installation path.

Now to link with it in a code project, I used the following with g++: g++ <other options> -g -D_GLIBCXX_DEBUG I/path/to/install_debug/include -L/path/to/install_debug/lib -lyaml-cpp

To install the default version, I used the following cmake command: cmake -DCMAKE_INSTALL_PREFIX=/path/to/install_default, and the following command to link: g++ <other options> I/path/to/install_default/include -L/path/to/install_default/lib -lyaml-cpp

This way two static libraries are built separately, and there are no conflicts.