gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Overwriting memory??

#15085 - Dreamer - Fri Jan 16, 2004 10:51 pm

I am about ten seconds from tearing off my own head. I've spent hours trying to figure out this problem I'm having with my code. I've been fiddling with sources and tutorials, and after a long time spent working in Mode 1 I discovered (unless I'm mistaken) that I didn't know how to show full screen images, so I had to switch to Mode 4, where I had examples.

So I've been working in it just fine until I tried to work on my Tetris game again (for practice). I have a strip of images for blocks and their 'animated' (read: blinking) frames, and I load two of them into memory and store them separately. I then made it so that if you press A they show their respective second frame, if you press B, their first. This is just to get them ready for slight blinking animation, so I marked the tiles to distinguish frames one and two.

However, while they both start out just fine, pressing the A button displays the second block's second frame for both blocks, instead of their own second frames, and pressing B shows the second block's first frame. I've been staring at this for hours trying to fix things and make sense of it, but no matter what I do it seems like the second block overwrites the data for the first block somehow, and I don't know how to fix it.

Here's a URL to the source (fairly simple), and I would greatly appreciate any insight. The sprite creation is in Functionality\Sprites.h and .c, and it is called from Main.c.

http://jhunix.hcf.jhu.edu/~prt2/Test/Test.zip

#15087 - sajiimori - Fri Jan 16, 2004 11:11 pm

Code:

Sprite Load ( u16 colorindex, u16 colors, u16 frames, u32 offset )
{
   u16 temp[frames];   // Temporary init of Frame array
   spr.u16_Frames               = temp;

The temp array is destroyed after Load returns, and you're saving a pointer to it. The outcome is undefined.

Quick solutions:

1) Change Sprite.u16_Frames to an actual array with MAX_FRAMES elements.

2) Dynamically allocate the array.
Code:

spr.u16_Frames = malloc(frames * sizeof(u16));

#15092 - Dreamer - Sat Jan 17, 2004 12:44 am

Greatly appreciate the help. Worked like a charm. Geez my C is rusty.

#15095 - sajiimori - Sat Jan 17, 2004 1:08 am

Glad to avert the impending head-tearage. =)