1

当我关闭我的应用程序时,我不断收到这个。我知道它与 JsonCpp 有关,因为它仅在我使用 Json 值时发生。

我认为这与我如何创建 json 值有关,但由于没有任何教程,我不知道应该怎么做

我的代码目前是:

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;
}

当我不这样做时,我应该添加这不会发生:root["blah"] = foo;

4

1 回答 1

2

编辑

发现这是一个已知问题(例如这里

原来这是由于 jsoncpp 中的某种错误导致它不能作为全局变量工作。我想全局变量是坏消息的旧观念很难摆脱。S* o 我把所有的 json 都从全局变量中解脱出来,现在它工作正常。少全局变量肯定是一个好的举措*,无论如何。

此处的错误报告(zeromus): http: //sourceforge.net/tracker/index.php ?func=detail&aid=2934500&group_id=144446&atid=758826

状态是固定的:

我通过给它一个 ValueAllocatorHandle 来表示一个 Value 对给定的 ValueAllocator 实例具有隐式依赖这一事实来修复它,它只是引用计数并在最后一个 Value 超出范围时负责删除堆分配的分配器。


一般来说,我的怀疑是构造函数/析构函数访问导致 UB 的虚拟成员(并且可能根本缺少虚拟析构函数)。

由于您提到应用程序关闭,static Json::Value root因此是一个嫌疑人。如果这是在 linux 上,我会在 valgrind 下运行它

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

这可确保您拥有必要的权限。当然,它有助于(很多)使用调试信息进行编译。

于 2011-10-31T00:14:10.447 回答