My memory is taking a leak

12-2-17

The prior solution to moving the character off screen worked for exactly one round of the game and then corruption happened. Took me a little while to find out what was happening, but it’s a classic memory leak. Every time the movecharacter function was called it made itself just a little larger. Eventually, it corrupts other parts of ram and on the next launch, it crashes.

The interesting thing is that CPPCheck (my go to c checker), did not see the error.

So, I decided there is more than one way to skin a cat. Another saying that is rather creepy, why would one want to skin a cat, has anyone in history made a fur coat made out of cat? Perhaps Cruella de Vil.

I’ll just pass an array to the function. So, it’s function writing time again.

YAH!

First I’m going to declare a short array at the start of the program.

short positions[6]={0,0,0,0,0,0};

6 elements are all we need for this function. I of course default the array slots to 0’s.

To populate and call the function is a little awkward

positions[0] = vx;
positions[1] = ax;
positions[2] = x;
positions[3] = vy;
positions[4] = ay;
positions[5] = y;
moveCharacter(positions);
vx = positions[0];
ax = positions[1];
x = positions[2];
vy = positions[3];
ay = positions[4];
y = positions[5];

I am just assigning a particular value to a slot in the array, I then push the entire array to the moveCharacter function, where the values are processed. Finally, I take the array and place them back into the variables. This can all be placed in brackets and tucked away neatly.

The function is also changed.

void moveCharacter (short positions[])
{
	//maxvx should be a constant
	//maxvy should be a constant
	
	short vX1 = positions[0];
	short aX1 = positions[1];
	short X1 = positions[2];
	short vY1 = positions[3];
	short aY1 = positions[4];
	short Y1 = positions[5];
	
	
	// motion of the protagonist
	if( Input() & 0x01 ) // P - go right
	{	
		aX1 = vX1 < maxvx ? 40 : 0;
 	} 	 	
	if( Input() & 0x02 ) // O - go left
 	{
	 	aX1 = vX1 > -maxvx ? -40 : 0;
	}
	
	//now we have down doing the same as up for testing
	if( Input() & 0x04 )// A go down
	{
		aY1 = (vY1 < maxvy) ? 40 : 1;
 	}
 	if( Input() & 0x08 )// Q go up
 	{
 		aY1 = (vY1 > -maxvy) ? -40 : -1;
	}

	//returns
	positions[0] = vX1;
	positions[1] = aX1;
	positions[2] = X1;
	positions[3] = vY1;
	positions[4] = aY1;
	positions[5] = Y1;
}

Compile, and play twice, even 3 times to test to make sure there are no memory leaks, seems good.

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s