943,922 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 817
  • C++ RSS
Apr 14th, 2009
0

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

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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 :
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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, 23 views)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Apr 14th, 2009
0

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

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.
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 14th, 2009
0

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

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++...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 14th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by ArkM ...
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..
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Apr 14th, 2009
0

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

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.
C++ Syntax (Toggle Plain Text)
  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, 23 views)
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Apr 14th, 2009
0

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

"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..
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Round double to N decimal places.
Next Thread in C++ Forum Timeline: L.SYSTEM





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC