| | |
Weird fstream problem when access member of a object inside other class..
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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 :
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:
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..
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:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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 :
C++ Syntax (Toggle Plain Text)
while((comm=readPNM.get()) != ' '){ strnum[i++] = comm; }
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:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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..
you did't include "F:\SDL-1.2.13\include\SDL.h in that zip file.
FiltroPNM::ApplyPixMap() has a lot of conversion problems -- converting int to float.
All those lines produce warnings which need to be fixed. And there are lots more similar problems all throughout that *.cpp code.
FiltroPNM::ApplyPixMap() has a lot of conversion problems -- converting int to float.
C++ Syntax (Toggle Plain Text)
media = ( F_img_pix[i].rgb[0] + F_img_pix[i].rgb[1] + F_img_pix[i].rgb[2]) * 0.33333; F_img_pix[i].rgb[0] = media;//transform red F_img_pix[i].rgb[1] = media;//transform green F_img_pix[i].rgb[2] = media;//transform blue
Last edited by Ancient Dragon; Apr 14th, 2009 at 11:32 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
You declare yet another local i in if alternative of CarregaPNM::ReturnPNM_header. This declaration hides variable i - loop counter. It's a very strange and most probably erroneous declaration. It seems the program logic is corrupped...
Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++...
Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++...
•
•
•
•
You declare yet another local i in if alternative of CarregaPNM::ReturnPNM_header. This declaration hides variable i - loop counter. It's a very strange and most probably erroneous declaration. It seems the program logic is corrupped...
Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++...
I just changed it to 'j' and the same problem happens..
•
•
•
•
you did't include "F:\SDL-1.2.13\include\SDL.h in that zip file.
FiltroPNM::ApplyPixMap() has a lot of conversion problems -- converting int to float.
All those lines produce warnings which need to be fixed. And there are lots more similar problems all throughout that *.cpp code.C++ Syntax (Toggle Plain Text)
media = ( F_img_pix[i].rgb[0] + F_img_pix[i].rgb[1] + F_img_pix[i].rgb[2]) * 0.33333; F_img_pix[i].rgb[0] = media;//transform red F_img_pix[i].rgb[1] = media;//transform green F_img_pix[i].rgb[2] = media;//transform blue
Yeah, Im getting +- 50 warnings about float conversion, I thought it was ok cause I need that conversion..Whats the best way to make this?
By the way this Filter class works ok too, and Im not using it at now..
![]() |
Other Threads in the C++ Forum
- Previous Thread: Round double to N decimal places.
- Next Thread: L.SYSTEM
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






