5

I have downloaded and installed the jsoncpp library. I then try to use the library in my own application:

#include <json/json.h>

void parseJson() {
   Json::Reader reader;
} 

int main(int argc, char ** argv) {
   parseJson();
   exit(0);
}

The program compiles and links fine, but it crashes with SIGSEGV when running. The gdb backtrace looks like this:

(gdb) bt
#0  0x0000003a560b7672 in __gnu_cxx::__exchange_and_add () from /usr/lib64/libstdc++.so.6
#1  0x00000000004031e9 in std::string::_Rep::_M_dispose (this=0xffffffffffffffe9, __a=@0x7fffbfe60e57)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:232
#2  0x0000000000403236 in ~basic_string (this=0x7fffbfe60fb0)
at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:478
#3  0x00000000004038d4 in ~Reader (this=0x7fffbfe60eb0) at /private/joaho/Parser/opm-parser/external/json/json-cpp/include/json/reader.h:23
#4  0x0000000000402990 in parseJson () at /private/joaho/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:51
#5  0x00000000004029ab in main (argc=1, argv=0x7fffbfe610c8)
at /home/user/Parser/opm-parser/opm/parser/eclipse/ExternalTests/ExternalTests.cpp:56

I.e. to me it seems to crash in the destructor. As far as I can tell the Json::Reader does not have it's own dstructor, so this must be a default destructor. As you can see I am running a quite old version of g++ - could that be the problem?

user422005
  • 1,989
  • 16
  • 34
  • 1
    When compiled with GCC version 4.8.1 on Debian/Sid (so libjsoncpp-dev `0.6.0~rc2-3`) as `g++-4.8 -g -Wall -I/usr/include/jsoncpp/ esjson.cc -ljsoncpp -o esjson` your program is compiled without warnings, and does not crash when running. – Basile Starynkevitch Jul 28 '13 at 18:35
  • I had this exact problem in VS2013. It came from not linking to the debug version of the library when doing a debug build. – matth Sep 21 '15 at 18:38
  • I have seen this issue in a bit of a spaghetti code project I have that statically links in webrtc (which links in jsoncpp) and also includes jsoncpp dynamically through another library. – mpr Jan 09 '17 at 23:02

1 Answers1

1

As I commented:

When compiled with GCC version 4.8.1 on Debian/Sid (so libjsoncpp-dev 0.6.0~rc2-3) as g++-4.8 -g -Wall -I/usr/include/jsoncpp/ esjson.cc -ljsoncpp -o esjson your program is compiled without warnings, and does not crash when running.

And GCC 4.1.2 is really old (febr. 2007 !) and is not supported anymore, and not very well C++ standard conforming (GCC, now at version 4.8.1, has made huge progress on C++ standard conformance since 4.1).

So I am not sure GCC 4.1. is faulty, but I won't be surprised it is: it had bad C++ reputation, and both the C++ standard and the GCC compiler have been improved a lot since that. Upgrading your GCC is worth the effort, both for better support of C++ and for improved diagnostics and optimizations.

So I suggest you to use a newer GCC; if you don't have root access, consider compiling its from its source tarball; build it outside of the source tree with e.g. ../gcc-4.8.1/configure --program-suffix=-4.8 --prefix=$HOME/pub then make then make install - after having installed its dependencies

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you for the effort; I guess I will blame the old gcc compiler. The reason I am slightly reluctant to install a newer compiler is that I am in a veeery conservative corporate environment, and the applications I write must work on the runtime (i.e. gcc-4.1.2) distributed throughout the organisation. My json needs are quite simple, so maybe I will try to find another (pure C?) library. – user422005 Jul 28 '13 at 18:50
  • There are other good reasons to advocate, within your corporation, the use of a newer GCC compiler: better standard conformance, better optimizations, better diagnostics.... Using GCC 4.1 for any kind of C++ programming is asking for deep trouble. (Most compiler bugs are *harder to find* than this one). – Basile Starynkevitch Jul 28 '13 at 18:53