I dont know how to explain very well, I have a class that works 100% fine..
The problem is that now I want access that class inside of other class, when I do it, things stop of working fine and gets very unstable( some few times works, but mostly times dont)..
Here is what I get:
Unhandled exception at 0x1049993d (msvcp90d.dll) in pnm.exe: 0xC0000005: Access violation reading location 0xfefeff02.
Now I will show the working methods where I use the ifstream, this works just fine if I use the object declared directly on main:
bool CarregaPNM::OpenPNM( char userstring[100] ){
//this was on constructor
for(unsigned short int i=0; i<100; i++){
_filename[i] = NULL;//clean the array(precaution)
}
//copy the user inputed string to the class private string:
strcpy_s(_filename, (const char*)userstring);
//
//if users tries to open another file without closing the file first, clear it:
if( fileclosed==false)readPNM.clear();
//open the file with the specified name:
readPNM.open( (const char*)_filename, ios::binary);
if( readPNM.is_open() ){
fileclosed = false;//file is not closed(is open)
//gets width and height of the image----------------
ReturnPNM_header();
img_pix = new PIXELS[width*height];//set the pixel matrix with the appropriated size
LoadPixels();//read the pixel map
return (true);//sucsses
}else return (false);//failed
}//F OpenPNM
void CarregaPNM::ReturnPNM_header(){
int countspaces=0;
char skipcomment[200];
char comm;
int auxpos=0;
for( int i=0; countspaces<4; i++ ){//until 3 spaces
readPNM>>comm;
if(comm != '#'){//if is not a comment
int i;
switch (countspaces){
case 0:
readPNM.unget();
readPNM >> MV[0] >> MV[1];
countspaces++;
break;
case 1:{
char strnum[10];
strnum[0] = comm;
i=1;
while((comm=readPNM.get()) != ' '){
strnum[i++] = comm;
}
strnum[i] = '\0';
width = atoi(strnum);
countspaces++;
}
break;
case 2:{
char strnum2[10];
strnum2[0] = comm;
i=1;
while((comm=readPNM.get()) != '\n'){
strnum2[i++] = comm;
}
strnum2[i] = '\0';
height = atoi(strnum2);
countspaces++;
}
break;
case 3:
if( MV[1] == '1' || MV[1] == '4' ){//if is PBM, theres no MPV
countspaces++;
break;
}
readPNM.unget();
readPNM >> MPV ;
countspaces++;
break;
}//F switch countspaces
}//F if nao for comentario
else{
readPNM.getline(skipcomment, 200);//if is a comment, skip the line
}
}//F for countspaces <=3
}
The error happens at the ReturnPNM_header(), what happens is that I get very weird values from ifstream, like it arent reading the right memory stuff, so when it takes at :
while((comm=readPNM.get()) != ' '){
strnum[i++] = comm;
}
It never find a space, it do forever until access violation...
Remember again, that it works FINE when I use directly on main..But I dont know what happens here, since it just get there if the file is open...Im very lost..
Now Im showing where Im using when get the error:
class InterfacePNM{
private:
...
CarregaPNM meuPNM;
public:
InterfacePNM();//constructor
bool Init_SDL();
bool LoadInterfaceImages();
void Handle_SDL_Events();
void UpdateKeyChanges();
void UpdateChanges();
bool _Open_();
//void _Save_();
//void _Filter_();
void MainLoop();
};//F Interface PNM
bool InterfacePNM::_Open_(){
char inputfilename[100];
cin >> inputfilename;
if( meuPNM.OpenPNM( inputfilename ) == false ) return false;
B_open = false;
return true;
}
My guess is that Im doing some Class management shit...
Im attaching all the code and the image files if anyone want to test, Im also attaching SDL lib and dll since Im using it in that program..