Search code examples
c++visual-studio-2010visual-studio-2008incompatibility

Program works fine in VS 2008, throws exception in VS 2010 at the very end of program


I'm writing to a file using binary/direct access. Works like a charm in 2008, but creates an exception in 2010 at runtime on line 128: Unhandled exception at 0x535dad54 (msvcp100d.dll) in HovedProsjekt.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

Clicking the error takes me to xutility, where line 201: _Pnext != 0; *_Pnext = (_Pnext)->_Mynextiter) is next to be executed. I looked at microsofts "breaking changes" page with no luck. I am copy/pasting the entire code in cleartext, so no outdated libraries. I have tried all combinations of enabling and disabling _HAS_ITERATOR_DEBUGGING and _SECURE_SCL. I changed my question on advice from another user to trim my code down.

//Miniprosjekt.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool sjekkForOverskriving(void);
void LagFil(void);
void LeggTil();
void skrivUtFeil();

class Feil{
    friend ostream& operator<<(ostream&, Feil);
    friend istream& operator>>(istream&, Feil&);
protected:
    int ID;
    int prioritet;
    string dato;
    char beskrivelse[300];
    char skrevetAv[30];
public:
    Feil();
    Feil(int, int, string, string, string);
};

Feil::Feil(){ //Default constructor
    ID = 0;
    prioritet = 1;
    dato = "01.01.1900";
    beskrivelse[0] = ' ';
    skrevetAv[0] = ' ';
}

Feil::Feil(int id, int pri, string d, string beskr, string navn){ //Constructor
    ID = id;
    prioritet = pri;
    dato = d;
    strcpy(beskrivelse, beskr.c_str());
    strcpy(skrevetAv, navn.c_str());
}


ostream& operator<<(ostream& out, Feil enfeil) //Overloader << operator, allowing for: cout << Feil afeil
{
    out << "Id nr: " << enfeil.ID << "\nSkrevet av: " << enfeil.skrevetAv  << "\nDato: " <<
        enfeil.dato << "\nPrioritet: ";
    if(enfeil.prioritet == 1)out << "Lav";
    else if(enfeil.prioritet == 2)out << "Middels";
    else out << "Høy";
    out << "\nBeskrivelse:\n" << enfeil.beskrivelse << endl;
    return out;
}

istream& operator>>(istream& in, Feil& enfeil) //Overloader >> operator: allowing for cin >> Feil afeil
{
    in >> enfeil.ID >> enfeil.dato >> enfeil.beskrivelse >> enfeil.skrevetAv;
    return in;
}

int main(void){

    LagFil();
    LeggTil();
    skrivUtFeil();

    return 0;
}



void LagFil(void){ //Creates a file with 500 empty "feil" objects
    const int MAXFEIL = 500;
    Feil enfeil;
    ofstream utFil;
    utFil.open("FeilTeller.dat");
    utFil << 0;
    utFil.close();
    utFil.open("Feilmeldinger.dat", ios::out | ios::binary);
    for(int a = 0; a <= MAXFEIL; a++){ //Fills the file with empty "Feil" objects
        utFil.write(reinterpret_cast <const char*>(&enfeil), sizeof(enfeil));
    }
    utFil.close();
}

void LeggTil(){
    int ID;
    ifstream innFil;
    innFil.open("FeilTeller.dat");
    innFil >> ID;
    innFil.close();
    fstream fil;
    fil.open("FeilTeller.dat");
    ID++; //Opens feilteller.dat, reads how many objects have already been created. Adds one to that to append.
    fil << ID;
    fil.close();
    int pri = 1;
    string beskrivelse = "test";
    string dato = "test2";
    string skrevetAv = "test3";
    Feil enfeil(ID, pri, dato, beskrivelse, skrevetAv);
    fil.open("Feilmeldinger.dat", ios::out | ios::in | ios::binary);
    fil.seekp((ID - 1) * sizeof(enfeil));
    fil.write(reinterpret_cast <const char*>(&enfeil),
            sizeof(Feil));
    fil.close();
}

void skrivUtFeil(){
    int idnr = 1;
    //for(int i = 0; i < 2; i++){
    Feil enfeil;
    int antallFeil;
    ifstream innFil;
    innFil.open("FeilTeller.dat");
    innFil >> antallFeil;
    innFil.close();
    innFil.open("Feilmeldinger.dat", ios::in | ios::binary);
    innFil.read(reinterpret_cast <char*>(&enfeil),
            sizeof(Feil));
    innFil.clear();
    //Prints out Feil with ID "idnr"
    innFil.seekg((idnr - 1) * sizeof(enfeil));
    innFil.read(reinterpret_cast <char*>(&enfeil),
            sizeof(Feil));
    cout << enfeil << endl;
    cout << "Totalt antall feil: " << antallFeil << endl;
    system("pause");
    innFil.close();
    //}
} //Exception on this line

Solution

  • Your class Feil contains string dato, so it's not trivially copyable. Therefore it's illegal to write and read it as raw binary data.

    What happens if you do it? std::string contains a pointer to character array, so you write its value, not content. When you read it, you read the same value into the pointer, but it's pointing to garbage.