Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault (core dumped) #309

Closed
sulliwane opened this issue Sep 13, 2016 · 7 comments
Closed

Segmentation fault (core dumped) #309

sulliwane opened this issue Sep 13, 2016 · 7 comments
Labels
solution: invalid the issue is not related to the library

Comments

@sulliwane
Copy link

Hello,

If I add this to my code:

#include "json.hpp"
using json = nlohmann::json;

    json sample;
    sample["sensor"] = "name";
    sample["position"]["x"] = 10.2;
    sample["position"]["y"] = 2.4;
    sample["position"]["z"] = 4.5;
    std::cout << sample.dump() << std::endl;

the last line sample.dump() will throw the error Segmentation fault (core dumped).

This is a node c++ addon, which binding.gyp is:

  "targets": [
    {
      "target_name": "node-addon",
      "sources": [ "src/node-addon.cpp" ],
      "cflags": ["-Wall", "-std=c++11", "-fexceptions"],
      "cflags_cc": ["-fexceptions"],
      "include_dirs" : ["<!(node -e \"require('nan')\")"],
    }
  ]

Any help would be much appreciated :)

Many thanks!

@whackashoe
Copy link
Contributor

whackashoe commented Sep 13, 2016

Just tested this, unable to reproduce.

test.cpp

#include <iostream>                                                                                                                 
#include "../src/json.hpp"
using json = nlohmann::json;

int main()         
{                  
    json sample;   
    sample["sensor"] = "name";
    sample["position"]["x"] = 10.2;
    sample["position"]["y"] = 2.4;
    sample["position"]["z"] = 4.5;
    std::cout << sample.dump() << std::endl;

    return 0;         
}

g++ -Wall -std=c++11 -fexceptions test.cpp -o test

results in:

{"position":{"x":10.2,"y":2.4,"z":4.5},"sensor":"name"}

This is on gcc version 4.9.3 (Ubuntu 4.9.3-8ubuntu2~14.04)

@sulliwane
Copy link
Author

@whackashoe Thanks for your fast reply.

My config is gcc (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609, but it seems that the root of the problem doesn't come from the json lib indeed.

My program seemed to run well until now, but as soon as I call sample.dump() from the json lib, then it's Segmentation fault (core dumped), If I don't call dump() method, I can construct a JSON, or even parse a JSON.

That's weird... :(

@whackashoe
Copy link
Contributor

Just to clarify, running the code I posted alone works? If so, you likely have memory corruption issues somewhere else.

@sulliwane
Copy link
Author

Yes, your code works perfectly.

Ok, so my problem must be somewhere else. What is strange is that if I don't run sample.dump(), then my program works perfectly (it doesn't make sense to me, but I'm noob in c++).

@nlohmann
Copy link
Owner

Can you run your code with Valgrind or Clang's memory sanitizer to make sure it is not a memory corruption?

@sulliwane
Copy link
Author

I run with Clang msan option -fsanitize=memory, it failed as well. So I copied my code in a new directory, reinstalled some dependencies and now it works without a glitch. There must have been an error somewhere, maybe a git diff will solve the mistery! anyway, many thanks for your time :)

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Sep 13, 2016
@kekkokk
Copy link

kekkokk commented Mar 11, 2019

I run with Clang msan option -fsanitize=memory, it failed as well. So I copied my code in a new directory, reinstalled some dependencies and now it works without a glitch. There must have been an error somewhere, maybe a git diff will solve the mistery! anyway, many thanks for your time :)

we are facing the same issue. no probl on ubuntu 14, only on ubuntu 18. it doesn't makes any sense. any more info on how you solved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

4 participants