# Cleaning up a Leek (leak)

12-4-2017

Memory leaks are kinda like the vegetable leek, they both need to be cleaned prior to usage, otherwise, you leave with a bad taste in your mouth.

In a previous entry, I had placed the edge checking routine as a pointer type function which seemed to leak memory to corrupt the remaining data. This was apparent when you finished a game round and killed all enemies, the game would crash.

So it’s now time to clean out that routine.

let’s start by declaring 2 more fixed sized arrays.

``````short horizonal  = {0,0,0};
short vertical  = {0,0,0};
``````

Next, we will come to the actual functions.

``````void verticalEdge (short vertical[])
{
//mapx is char
//mapy is char
short vY1 = vertical;
short aY1 = vertical;
short Y1 = vertical;

vY1 += aY1;
Y1 += vY1;

if( vY1 + 8 >> 3 )
{
aY1 = -vY1 >> 3;
}
else
{
aY1 = vY1 = 0;
}

if( (unsigned int)Y1 > scrh << 12 )
{
if( vY1 > 0 )
{
if( mapy < maph - 1 )
{
Y1 = scrh << 12;
vY1= 0;
}
}

//screen left
if( vY1 < 0 )
{
if( mapy < 8)
{
Y1 = 0;
}
}
}

//returns
vertical = vY1;
vertical = aY1;
vertical = Y1;
}
``````

and now the horizontal function

``````void horizontalEdge (short horizonal[])
{
//mapx is char
//mapy is char

short vX1 = horizonal;
short aX1 = horizonal;
short X1 = horizonal;

vX1 += aX1;
X1 += vX1;

if( vX1 + 8 >> 3 )
{
aX1 = -vX1 >> 3;
}
else
{
aX1 = vX1 = 0;
}

if( (unsigned int)X1 > scrw << 12 ) 	{	 		if( vX1 > 0 )
{
//screen right
if( mapx < mapw - 2 )
{
X1 = scrw << 12;
}
}

//screen left
if( vX1 < 0 )
{
if( mapx < 16)
{
//X1 = 0 << 12;
X1 = 24;
}
}
}

//returns
horizonal = vX1;
horizonal = aX1;
horizonal = X1;
}
``````

Finally, comes our calling routines

``````//check edges
{
//vertical edge
vertical = vy;
vertical = ay;
vertical = y;
verticalEdge(vertical);
vy = vertical;
ay = vertical;
y = vertical;

//horizontal edge
horizonal = vx;
horizonal = ax;
horizonal = x;
horizontalEdge(horizonal);
vx = horizonal;
ax = horizonal;
x = horizonal;
}
``````

Is the cure worse than the disease? It is, of course, a change in coding style, it does clean up the main loop a bit, plus if I wanted to add more items to the functions, I could. It is really no better than the original routine, but I personally prefer this, as it makes it a little easier on my eyes and to separate the functions. 