I've been trying to solve this for days now, and can't get it to work. I'm getting a access violation in the method "orphan_all" in a std::string destructor that's called from a compiler-generated POD struct, that contains some 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;
};
That's the structure. When the destructor of that object is called, here:
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);
}
A access violation happends inside "orphan_all",that is called from the destructor of the strings inside "Data" inside "fdata", and it says locations like "0xdddddddd" or "0xFEEEFEEE", so by some reason it appears to call some deleted data. I checked for heap corruption using HeapValidate() and _CrtCheckMemory() and everything seems to be fine. If I compile in release, the problem goes away. Anyone has any idea? My system is Windows 8 Pro x64, using Visual Studio Express 2012, compiling with the v110 toolset.
EDIT: I'm writing the data like this:
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();
}