我一直试图解决这个问题已经好几天了,而且无法让它发挥作用。我在std :: string析构函数中的方法“orphan_all”中遇到访问冲突,该析构函数是从编译器生成的POD结构中调用的,该结构包含一些std :: string。
struct SaveData
{
SaveData()
{
MusicStage = GameState::MusicStage;
MusicSubStage= GameState::MusicSubStage;
PlotStage = GameState::PlotStage;
PlotSubStage = GameState::PlotSubStage;
GameStage = GameState::GameStage;
GameSubStage = GameState::GameSubStage;
PlayerLife = 100.0f;
PlayerSuitEnergy = 100.0f;
CurrentPower = 0;
PlayerPos = XMFLOAT3(0,0,0);
CurrentGun = 0;
Guns = 0;
ModsL1 = 0;
ModsL2 = 0;
ModsL3 = 0;
ModsL4 = 0;
CurrentBulletMod = (uint)BulletMod::NoMod;
ElectricModMult = 1.0;
ExplosiveModMult = 1.0;
CorrosiveModMult = 1.0;
}
string MusicStage;
string MusicSubStage;
string PlotStage;
string PlotSubStage;
string GameStage;
string GameSubStage;
float PlayerLife;
float PlayerSuitEnergy;
uint CurrentPower;
XMFLOAT3 PlayerPos;
uint CurrentGun;
uint CurrentBulletMod;
float ElectricModMult;
float ExplosiveModMult;
float CorrosiveModMult;
uint Guns;
uint ModsL1;
uint ModsL2;
uint ModsL3;
uint ModsL4;
};
struct FileData
{
uint64 Hash;
uint Version;
SaveData Data;
};
这就是结构。当调用该对象的析构函数时,这里:
HRESULT SavesIO::LoadGameFile(const std::string& FileName,SaveData& Data)
{
ifstream file;
file.open(FileName,ios::binary);
if(file.is_open())
{
FileData fdata;
file.read((char*)&fdata,sizeof(FileData));
if(fdata.Hash != GameHash)
{
cout << "Corrupt Savegame : " << FileName << endl;
return CheckHR(HR_Fail);
}
if(fdata.Version > CurrentVersion)
{
cout << "Savegame version is greater than game version : " << FileName << endl;
return CheckHR(HR_Fail);
}
Data = fdata.Data;
return HR_Correct;
}
cout << "Savegame : " << FileName << "not found" << endl;
return CheckHR(HR_Invalid_Arg);
}
访问冲突发生在“orphan_all”内部,这是从“fdata”内“数据”内部的字符串的析构函数调用的,它表示“0xdddddddd”或“0xFEEEFEEE”之类的位置,所以出于某种原因调用一些删除的数据。 我使用HeapValidate()和_CrtCheckMemory()检查堆损坏,一切似乎都没问题。如果我在发布中编译,问题就会消失。 任何人有任何想法? 我的系统是Windows 8 Pro x64,使用Visual Studio Express 2012,使用v110工具集进行编译。
编辑:我正在写这样的数据:void SavesIO::SaveGameFile(SaveData Data,const std::string& FileName)
{
ofstream file;
file.open(FileName,ios::binary);
FileData fdata;
fdata.Hash = GameHash;
fdata.Version = CurrentVersion;
fdata.Data = Data;
file.write((char*)&fdata,sizeof(FileData));
file.close();
}
答案 0 :(得分:1)
似乎_ITERATOR_DEBUG_LEVEL应该归咎于调试模式只会崩溃。
我不反对你的解决方案。相反,这是最好的事情。 但是,以下是GoodToKnow:
或者
或(甚至更好) 在项目的预编译器中定义它。
这将停止STL以触发异常.... 参考:https://msdn.microsoft.com/en-us/library/hh697468.aspx
确保使用相同的选项编译所有嵌套/依赖项目,或者:_iterator_debug_level value '0' doesn't match value '2'
此定义的默认值为2。
PS看起来MS仍在编译他们内部开发的STL,即使在工具集v120
答案 1 :(得分:0)
这是IInspectable所说的,但我无法将其标记为答案,因为这是一个评论。我的写作和阅读功能现在看起来像这样:
void SavesIO::SaveGameFile(SaveData Data,const std::string& FileName)
{
ofstream file;
file.open(FileName,ios::binary);
FileData fdata;
fdata.Hash = GameHash;
fdata.Version = CurrentVersion;
fdata.PlayerLife = Data.PlayerLife;
fdata.PlayerSuitEnergy = Data.PlayerSuitEnergy;
fdata.CurrentPower = Data.CurrentPower;
fdata.PlayerPos = Data.PlayerPos;
fdata.CurrentGun = Data.CurrentGun;
fdata.CurrentBulletMod = Data.CurrentBulletMod;
fdata.ElectricModMult = Data.ElectricModMult;
fdata.ExplosiveModMult = Data.ExplosiveModMult;
fdata.CorrosiveModMult = Data.CorrosiveModMult;
fdata.Guns = Data.Guns;
fdata.ModsL1 = Data.ModsL1;
fdata.ModsL2 = Data.ModsL2;
fdata.ModsL3 = Data.ModsL3;
fdata.ModsL4 = Data.ModsL4;
file.write((char*)&fdata,sizeof(FileData));
int size = Data.MusicStage.length();
file.write((char*)&size,sizeof(int));
file.write(Data.MusicStage.c_str(),Data.MusicStage.length()+1);
size = Data.MusicSubStage.length();
file.write((char*)&size,sizeof(int));
file.write(Data.MusicSubStage.c_str(),Data.MusicSubStage.length()+1);
size = Data.PlotStage.length();
file.write((char*)&size,sizeof(int));
file.write(Data.PlotStage.c_str(),Data.PlotStage.length()+1);
size = Data.PlotSubStage.length();
file.write((char*)&size,sizeof(int));
file.write(Data.PlotSubStage.c_str(),Data.PlotSubStage.length()+1);
size = Data.GameStage.length();
file.write((char*)&size,sizeof(int));
file.write(Data.GameStage.c_str(),Data.GameStage.length()+1);
size = Data.GameSubStage.length();
file.write((char*)&size,sizeof(int));
file.write(Data.GameSubStage.c_str(),Data.GameSubStage.length()+1);
file.close();
}
我的阅读:
HRESULT SavesIO::LoadGameFile(const std::string& FileName,SaveData& Data)
{
ifstream file;
file.open(FileName,ios::binary);
if(file.is_open())
{
FileData fdata;
file.read((char*)&fdata,sizeof(FileData));
if(fdata.Hash != GameHash)
{
cout << "Corrupt Savegame : " << FileName << endl;
return CheckHR(HR_Fail);
}
if(fdata.Version > CurrentVersion)
{
cout << "Savegame version is greater than game version : " << FileName << endl;
return CheckHR(HR_Fail);
}
Data.PlayerLife = fdata.PlayerLife;
Data.PlayerSuitEnergy = fdata.PlayerSuitEnergy;
Data.CurrentPower = fdata.CurrentPower;
Data.PlayerPos = fdata.PlayerPos;
Data.CurrentGun = fdata.CurrentGun;
Data.CurrentBulletMod = fdata.CurrentBulletMod;
Data.ElectricModMult = fdata.ElectricModMult;
Data.ExplosiveModMult = fdata.ExplosiveModMult;
Data.CorrosiveModMult = fdata.CorrosiveModMult;
Data.Guns = fdata.Guns;
Data.ModsL1 = fdata.ModsL1;
Data.ModsL2 = fdata.ModsL2;
Data.ModsL3 = fdata.ModsL3;
Data.ModsL4 = fdata.ModsL4;
int size = 0;
file.read((char*)&size,sizeof(int));
char* tmp = new char[size+1];
file.read(tmp,size+1);
Data.MusicStage = tmp;
delete tmp;
size = 0;
file.read((char*)&size,sizeof(int));
tmp = new char[size+1];
file.read(tmp,size+1);
Data.MusicSubStage = tmp;
delete tmp;
size = 0;
file.read((char*)&size,sizeof(int));
tmp = new char[size+1];
file.read(tmp,size+1);
Data.PlotStage = tmp;
delete tmp;
size = 0;
file.read((char*)&size,sizeof(int));
tmp = new char[size+1];
file.read(tmp,size+1);
Data.PlotSubStage = tmp;
delete tmp;
return HR_Correct;
size = 0;
file.read((char*)&size,sizeof(int));
tmp = new char[size+1];
file.read(tmp,size+1);
Data.GameStage = tmp;
delete tmp;
return HR_Correct;
size = 0;
file.read((char*)&size,sizeof(int));
tmp = new char[size+1];
file.read(tmp,size+1);
Data.PlotSubStage = tmp;
delete tmp;
return HR_Correct;
size = 0;
file.read((char*)&size,sizeof(int));
tmp = new char[size+1];
file.read(tmp,size+1);
Data.GameSubStage = tmp;
delete tmp;
return HR_Correct;
}
cout << "Savegame : " << FileName << "not found" << endl;
return CheckHR(HR_Invalid_Arg);
}
我还有最后一个问题。如果用户试图将他的保存从一台32位计算机转移到64位计算机,我可以遇到什么问题? Endianess可能不是问题,因为该游戏仅限于Windows。 无论如何,谢谢你,给了我很多时间。