gpt4 book ai didi

c++ - 使用 JSonCpp 调用纯虚函数

转载 作者:可可西里 更新时间:2023-11-01 12:03:11 25 4
gpt4 key购买 nike

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

最佳答案

编辑

发现这是一个已知问题(例如 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.

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

状态是固定的:

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.


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

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

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

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

关于c++ - 使用 JSonCpp 调用纯虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7948821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com