C++ 使用JSonCpp进行纯虚拟函数调用

C++ 使用JSonCpp进行纯虚拟函数调用,c++,windows,runtime-error,jsoncpp,C++,Windows,Runtime Error,Jsoncpp,当我关闭我的应用程序时,我总是收到这个。 我知道这与JsonCpp有关,因为它只在我使用Json值时发生 我认为这与我如何创建json值有关,但由于没有任何教程,我不知道应该如何创建json值 我的代码当前为: static Json::Value root; // will contains the root value after parsing. unsigned int WindowSettings::WindowWidth = 800; unsigned int WindowSe

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

我认为这与我如何创建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;
}
静态Json::Value root;//将包含解析后的根值。
无符号int-WindowSettings::WindowWidth=800;
无符号int-WindowSettings::WindowHeight=600;
bool WindowSettings::FullScreen=false;
无符号短窗口设置::抗锯齿=16;
bool WindowSettings::VSync=false;
短窗口设置::FrameRateLimit=60;
AspectRatios窗口设置::AspectRatio=ar4p3;
分辨率窗口设置::分辨率=r800x600;
Json::Value WindowSettings::root=Json::Value();
void WindowSettings::remakefault()
{
根[“WindowWidth”]=WindowWidth;
根[“窗高”]=窗高;
根[“全屏”]=全屏;
根[“抗锯齿”]=抗锯齿;
根[“VSync”]=VSync;
根[“FrameRateLimit”]=FrameRateLimit;
根[“AspectRatio”]=AspectRatio;
根[“分辨率”]=分辨率;
saveToFile(“conf.json”);
}
bool WindowSettings::saveToFile(const std::string和fileName)
{
Json::FastWriter;
//为配置创建新的JSON文档。保留原始注释。
std::string outputConfig=writer.write(root);
std::流myfile;
myfile.open(fileName.c|u str(),std::ios::out | std::ios::trunc | std::ios::binary);
如果(myfile.is_open())
{
我的文件编辑

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

原来这是由于jsoncpp中存在某种缺陷,使得它不能作为全局变量工作。我想,这是一个很难摆脱的旧观念,即全局变量是坏消息。S*o我把我所有的json都包装起来,不再是全局变量,现在它工作得很好。不管是什么情况,减少全局变量肯定是一个好的举措*

此处的错误报告(zeromus):

状态是固定的:

我已经修复了这个问题,它表示了一个值对ValueAllocator的给定实例具有隐式依赖性,给它一个ValueAllocatorHandle,它只是引用计数,并在最后一个值超出范围时负责删除堆分配的分配器


一般来说,我的怀疑是一个构造函数/析构函数访问虚拟成员,导致UB(可能根本就没有虚拟析构函数)

既然您提到应用程序关闭,
staticjson::Value root
是一个可疑的问题。如果这是在linux上,我会在valgrind下运行它

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

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

…没有
根[“blah”]=foo
?!我目前在windows上使用VS2010,我已经在调试模式下运行了它,但它并没有给我太多信息。如果需要,使用break函数可以转到这里(rternum!=\u-RT\u-CRNL&&rternum!=\u-RT\u-BANNER&&rternum!=\u-RT\u-CRT\u-NOTINIT){switch(\u-CrtDbgReportW(\u-CRT\u-ERROR,NULL,0,NULL,ERROR\u-text)){case 1:\u-crtdbgbbreak();msgshown=1;break;case 0:msgshown=1;break;}特别是案例1,你指的
root[“blah”]=foo
在哪里?root[“blah”]=foo我指的是remakefault中的所有内容。即root[“WindowWidth”]=WindowWidth;root[“WindowHeight”]=WindowHeight;root[“FullScreen”]=FullScreen;root[“AntiAliasing”]=AntiAliasing;root[“VSync”]=VSync;root[“FrameRateLimit”]=FrameRateLimit;root[“AspectRatio”]=AspectRatio;root[“Resolution”]=Resolution;@Richy19:堆栈跟踪低于其触发点。在调用堆栈中向上移动一(几)帧(
Debug/Views/Call stack
)您没有提到触发中断的原因。是否显示了调试断言对话框?请提及以下重要内容:)(哦,请不要将代码粘贴到注释中,尤其是不要粘贴多余的代码?谢谢)调用堆栈是:[link]很抱歉,我还是SOF新手,因此正在尝试学习诀窍