-3

I'm testing out a game mechanic where there will be two files (one called 'X.txt' and the other 'Y.txt') that keep track of your X and Y position. The way it works is that everytime an update for the player is called (i.e movement), the program will write the coordinates to the X and Y file. And when you close the program and reopen that file, you will start off at the X and Y coordinates that you were at when you last closed the program. I have two problems:

1) Everytime time I run the program I start at the default coordinates (0, 0) even though I'm telling the program to read from the file.

2) When I close the program I get an error saying :

'Unhandled exception at 0x576812A7 (msvcp120d.dll) in FNTSTWE.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.'

The error are variable by the way, they are always quite different, this is just one of the many errors I got.

Also, sometimes it leads me to different files like 'xlocale' or 'output.c'.

I think these two things are linked and if I can fix them I can get them both to work.

Here is the code : https://www.dropbox.com/s/j56kb41q3bmmnby/Program_SDL2_IO_Error.zip?dl=0

Just look at the code and please tell me what I'm doing wrong.

In order to recreate the errors I was talking about, use the files to create a Visual Studio SDL2 project and debug it to see what I mean.

Please Help.

Thank you.

  • 1
    You should provide [MCVE](http://stackoverflow.com/help/mcve) instead of whole project. – Zereges Sep 02 '15 at 10:53
  • ...and it should be compileable. Which means a complete solution in Visual Studio, and something with a Makefile in the g++ world. And so on. – Topological Sort Sep 02 '15 at 18:53

1 Answers1

1
  1. In your CReadWriter class' functions, you pass arguments as values, thus not changing the value of curX, and curY in CPlayer::CPlayer (They remain 0).
  2. In CPlayer::~CPlayer (destructor) you set the Image_Surface to NULL, then trying to free it with SDL_FreeSurface causing the memory errors.

Solutions:

  1. Please see this post, to learn the difference between the different ways of argument passing: What's the difference between passing by reference vs. passing by value?

Your read methods must be something like:

void ReadX(int* X); // pass a pointer to the variable, in which you want to store the read value
void ReadX(int& X); // pass a reference to the variable ....
  1. Change the order of the two statements in the destructor: first free the image surface, then set the variable to NULL.
Community
  • 1
  • 1
zaw
  • 220
  • 3
  • 12