Search code examples
c++c++11gdbc++14catch2

Shared library problems between C++11 and C++14


I have had to switch from C++11 to C++14 in my project to use the Catch testing framework, written for C++14 (it doesn't compile with anything less). Everything compiles fine. However, while running the program, the following function causes a series of errors which seem linked to a mismatch between C++11 and C++14 shared standard library files:

//it is called as such:
string p = "...";
handle_file(p);

//...
int handle_file (string p) {
        if (is_valid(p)) {
            return 0;
        }
        else return -1;
    }

The error trace in gdb reads:

Program received signal SIGABRT, Aborted.
0x00007ffff7299ef5 in raise () from /usr/lib/libc.so.6
(gdb) bt
#0  0x00007ffff7299ef5 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff7283862 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff72dbf38 in __libc_message () from /usr/lib/libc.so.6
#3  0x00007ffff72e3bea in malloc_printerr () from /usr/lib/libc.so.6
#4  0x00007ffff72e5113 in _int_free () from /usr/lib/libc.so.6
#5  0x00007ffff72e8ca8 in free () from /usr/lib/libc.so.6
#6  0x00007ffff76b76ea in operator delete (ptr=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/del_op.cc:49
#7  0x00007ffff76b76fa in operator delete (ptr=<optimized out>) at /build/gcc/src/gcc/libstdc++-v3/libsupc++/del_ops.cc:33
#8  0x000055555556c893 in __gnu_cxx::new_allocator<char>::deallocate (__t=<optimized out>, __p=<optimized out>, this=0x7fffffffd910)
    at /usr/include/c++/10.2.0/ext/new_allocator.h:133
#9  std::allocator_traits<std::allocator<char> >::deallocate (__n=<optimized out>, __p=<optimized out>, __a=...)
    at /usr/include/c++/10.2.0/bits/alloc_traits.h:492
#10 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_destroy (__size=<optimized out>, this=0x7fffffffd910)
    at /usr/include/c++/10.2.0/bits/basic_string.h:237
#11 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose (this=0x7fffffffd910)
    at /usr/include/c++/10.2.0/bits/basic_string.h:232
#12 std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string (this=0x7fffffffd910, __in_chrg=<optimized out>)
    at /usr/include/c++/10.2.0/bits/basic_string.h:658
#13 Manager::handle_file (p ="./data/20pix", this=0x5555556c6d20)
    at src/manager.hpp:149

Does anyone know how to solve this?


Solution

  • The error is inside malloc, after it has printed some error message (usually double-free or something similar) indicating heap corruption.

    While it is possible that you have some kind of library mismatch, it is much more likely that you have a plain heap corruption bug in your own code (overflowing heap buffer, double delete, delete of non-allocated, etc. etc.), and that bug is simply getting exposed now, while it remained hidden before.

    Your first step should be to run the program under Valgrind or with Address Sanitizer, and fix any errors these tools report.