Question

I keep getting this when I close my application. I know it has something to do with JsonCpp as it only happens when I use Json values.

I think its something to do with how I create the json value but as there aren't really any tutorials I have no idea how I should do it

My code is currently:

static Json::Value root;   // will contains the root value after parsing.

unsigned int WindowSettings::WindowWidth = 800;
unsigned int WindowSettings::WindowHeight = 600;
bool WindowSettings::FullScreen = false;
unsigned short WindowSettings::AntiAliasing = 16;
bool WindowSettings::VSync = false;
short WindowSettings::FrameRateLimit = 60;
AspectRatios WindowSettings::AspectRatio = ar4p3;
Resolutions WindowSettings::Resolution = r800x600;
Json::Value WindowSettings::root = Json::Value();

void WindowSettings::remakeDefault()
{
    root["WindowWidth"] = WindowWidth;
    root["WindowHeight"] = WindowHeight;
    root["FullScreen"] = FullScreen;
    root["AntiAliasing"] = AntiAliasing;
    root["VSync"] = VSync;
    root["FrameRateLimit"] = FrameRateLimit;
    root["AspectRatio"] = AspectRatio;
    root["Resolution"] = Resolution;
    saveToFile("conf.json");
}

bool WindowSettings::saveToFile(const std::string &fileName)
{
    Json::FastWriter writer;
    // Make a new JSON document for the configuration. Preserve original comments.
    std::string outputConfig = writer.write( root );

    std::ofstream myfile;
    myfile.open (fileName.c_str(), std::ios::out | std::ios::trunc | std::ios::binary );
    if (myfile.is_open())
    {
        myfile << outputConfig.c_str();
        myfile.close();
    }
    return true;
}

I should add this doesnt happen when I dont do the: root["blah"] = foo;

Was it helpful?

Solution

EDIT

Found this is a known problem (e.g. here)

Turns out this is due to some sort of bug in jsoncpp which makes it not work as global variables. Which I guess the old notion that global variables are bad news is hard to get away from. S*o I wrapped up all my json away from being globals and it works fine now. Less globals is certainly a good move*, whatever the case.

Bug report here (zeromus): http://sourceforge.net/tracker/index.php?func=detail&aid=2934500&group_id=144446&atid=758826

The status is FIXED:

I have fixed it by representing the fact that a Value has an implicit dependency on a given instance of ValueAllocator by giving it a ValueAllocatorHandle which is just reference counted and takes care of deleting a heap allocated allocator when the last Value goes out of scope.


In general my suspicion would be a constructor/destructor accessing virtual members leading to UB (and perhaps missing virtual destructor at all).

Since you mention app shutdown, static Json::Value root is a suspect. If this is on linux, I'd run it under valgrind

sudo -E valgrind --db--attach=yes ./yourprogram

This makes sure that you have the necessary permissions. Of course, it helps (a lot) to compile with debug information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top