【问题标题】:Pure virtual function call with JSonCpp使用 JSonCpp 进行纯虚函数调用
【发布时间】:2011-10-31 00:04:39
【问题描述】:

当我关闭我的应用程序时,我不断收到这个。 我知道它与 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;
}

当我不这样做时,我应该添加这不会发生: 根[“废话”] = foo;

【问题讨论】:

  • ....没有root["blah"] = foo?!

标签: c++ windows runtime-error jsoncpp


【解决方案1】:

编辑

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

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

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

状态是固定的:

我已经通过给它一个 ValueAllocatorHandle 来表示它对给定的 ValueAllocator 实例具有隐式依赖这一事实来解决它范围。


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

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

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

这可确保您拥有必要的权限。当然,使用调试信息进行编译会有很大帮助。

【讨论】:

  • 我目前在使用 VS2010 的 Windows 上,我已经在调试模式下运行它,但它并没有真正给我太多信息。使用中断函数将我带到这里 if (rterrnum != _RT_CRNL && rterrnum != _RT_BANNER && rterrnum != _RT_CRT_NOTINIT) { switch (_CrtDbgReportW(_CRT_ERROR, NULL, 0, NULL, error_text)) { case 1: _CrtDbgBreak(); msgshown = 1;休息;案例 0:msgshown = 1;休息; } } 具体案例 1
  • 那么,你指的root["blah"] = foo在哪里?
  • 通过 root["blah"] = foo 我的意思是 remakeDefault 中的所有内容。即 root["WindowWidth"] = WindowWidth;根[“窗口高度”] = 窗口高度;根[“全屏”] = 全屏;根[“抗锯齿”] = 抗锯齿;根[“垂直同步”] =垂直同步;根[“帧速率限制”] = 帧速率限制;根[“纵横比”] =纵横比; root["分辨率"] = 分辨率;
  • @Richy19:该堆栈跟踪低于它被触发的点。在调用堆栈中上升一个(几个)帧(Debug/Views/Call Stack)你没有提到触发中断的原因。是否显示了调试断言对话框?请提及类似的重要事项 :)(哦,请不要将代码粘贴到 cmets 中,尤其是不要粘贴冗余代码?谢谢)
  • 调用堆栈是:[link]pastebin.com/NTkLLTf9 对不起,我对 SOF 还是新手,所以试图学习绳索
猜你喜欢
  • 2011-08-06
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 2014-03-03
相关资源
最近更新 更多