Weird fstream problem when access member of a object inside other class..

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Weird fstream problem when access member of a object inside other class..

 
0
  #1
Apr 14th, 2009
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:
  1. bool CarregaPNM::OpenPNM( char userstring[100] ){
  2. //this was on constructor
  3.  
  4. for(unsigned short int i=0; i<100; i++){
  5. _filename[i] = NULL;//clean the array(precaution)
  6. }
  7.  
  8. //copy the user inputed string to the class private string:
  9. strcpy_s(_filename, (const char*)userstring);
  10.  
  11.  
  12.  
  13. //
  14.  
  15. //if users tries to open another file without closing the file first, clear it:
  16. if( fileclosed==false)readPNM.clear();
  17.  
  18. //open the file with the specified name:
  19. readPNM.open( (const char*)_filename, ios::binary);
  20.  
  21. if( readPNM.is_open() ){
  22.  
  23. fileclosed = false;//file is not closed(is open)
  24.  
  25.  
  26. //gets width and height of the image----------------
  27. ReturnPNM_header();
  28.  
  29. img_pix = new PIXELS[width*height];//set the pixel matrix with the appropriated size
  30.  
  31. LoadPixels();//read the pixel map
  32.  
  33. return (true);//sucsses
  34.  
  35. }else return (false);//failed
  36. }//F OpenPNM
  1. void CarregaPNM::ReturnPNM_header(){
  2.  
  3. int countspaces=0;
  4.  
  5. char skipcomment[200];
  6.  
  7. char comm;
  8.  
  9. int auxpos=0;
  10.  
  11.  
  12. for( int i=0; countspaces<4; i++ ){//until 3 spaces
  13.  
  14. readPNM>>comm;
  15.  
  16. if(comm != '#'){//if is not a comment
  17.  
  18.  
  19. int i;
  20.  
  21. switch (countspaces){
  22. case 0:
  23. readPNM.unget();
  24. readPNM >> MV[0] >> MV[1];
  25.  
  26. countspaces++;
  27.  
  28. break;
  29. case 1:{
  30. char strnum[10];
  31. strnum[0] = comm;
  32. i=1;
  33. while((comm=readPNM.get()) != ' '){
  34. strnum[i++] = comm;
  35. }
  36. strnum[i] = '\0';
  37. width = atoi(strnum);
  38.  
  39. countspaces++;
  40.  
  41. }
  42. break;
  43.  
  44.  
  45. case 2:{
  46. char strnum2[10];
  47. strnum2[0] = comm;
  48. i=1;
  49. while((comm=readPNM.get()) != '\n'){
  50. strnum2[i++] = comm;
  51. }
  52. strnum2[i] = '\0';
  53. height = atoi(strnum2);
  54.  
  55. countspaces++;
  56.  
  57. }
  58. break;
  59.  
  60.  
  61. case 3:
  62. if( MV[1] == '1' || MV[1] == '4' ){//if is PBM, theres no MPV
  63. countspaces++;
  64. break;
  65. }
  66.  
  67. readPNM.unget();
  68. readPNM >> MPV ;
  69.  
  70.  
  71. countspaces++;
  72. break;
  73. }//F switch countspaces
  74.  
  75. }//F if nao for comentario
  76. else{
  77. readPNM.getline(skipcomment, 200);//if is a comment, skip the line
  78.  
  79. }
  80.  
  81. }//F for countspaces <=3
  82.  
  83.  
  84. }

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 :
  1. while((comm=readPNM.get()) != ' '){
  2. strnum[i++] = comm;
  3. }
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:
  1. class InterfacePNM{
  2. private:
  3. ...
  4. CarregaPNM meuPNM;
  5.  
  6. public:
  7.  
  8. InterfacePNM();//constructor
  9.  
  10. bool Init_SDL();
  11. bool LoadInterfaceImages();
  12.  
  13. void Handle_SDL_Events();
  14. void UpdateKeyChanges();
  15. void UpdateChanges();
  16.  
  17. bool _Open_();
  18. //void _Save_();
  19. //void _Filter_();
  20.  
  21.  
  22. void MainLoop();
  23.  
  24. };//F Interface PNM
  1. bool InterfacePNM::_Open_(){
  2.  
  3. char inputfilename[100];
  4. cin >> inputfilename;
  5.  
  6. if( meuPNM.OpenPNM( inputfilename ) == false ) return false;
  7.  
  8. B_open = false;
  9.  
  10. return true;
  11.  
  12. }

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..
Attached Files
File Type: zip pnm.zip (658.9 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Weird fstream problem when access member of a object inside other class..

 
0
  #2
Apr 14th, 2009
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.
  1. media = ( F_img_pix[i].rgb[0] + F_img_pix[i].rgb[1] + F_img_pix[i].rgb[2]) * 0.33333;
  2.  
  3. F_img_pix[i].rgb[0] = media;//transform red
  4. F_img_pix[i].rgb[1] = media;//transform green
  5. F_img_pix[i].rgb[2] = media;//transform blue
All those lines produce warnings which need to be fixed. And there are lots more similar problems all throughout that *.cpp code.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Weird fstream problem when access member of a object inside other class..

 
0
  #3
Apr 14th, 2009
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++...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: Weird fstream problem when access member of a object inside other class..

 
0
  #4
Apr 14th, 2009
Originally Posted by ArkM View Post
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 dont get it..I though it was ok because the scope( and it was, since it never give me any problem..)
I just changed it to 'j' and the same problem happens..
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: Weird fstream problem when access member of a object inside other class..

 
0
  #5
Apr 14th, 2009
Originally Posted by Ancient Dragon View Post
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.
  1. media = ( F_img_pix[i].rgb[0] + F_img_pix[i].rgb[1] + F_img_pix[i].rgb[2]) * 0.33333;
  2.  
  3. F_img_pix[i].rgb[0] = media;//transform red
  4. F_img_pix[i].rgb[1] = media;//transform green
  5. F_img_pix[i].rgb[2] = media;//transform blue
All those lines produce warnings which need to be fixed. And there are lots more similar problems all throughout that *.cpp code.
I forgot the '.h' files..sorry.I guess I have to upload all sdl .h files rigth? Im attaching all sdl files..


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..
Attached Files
File Type: zip SDL-devel-1.2.13-VC8.zip (607.5 KB, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Re: Weird fstream problem when access member of a object inside other class..

 
0
  #6
Apr 14th, 2009
"Apropos, avoid illusions: this evil fstream knows nothing about your classes and no such beast as a class management in C++..."

I meant something like including the classes in a wrong way, so something like overwriting the ifstream readPNM could be happening..I dont know..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC