Thread: Tripple crash program

  1. #1
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110

    Bizzare Crashing

    Alright, on startup the program run Init() which has the following relevant code:
    Code:
    for(int y = 0; y < BOARD_HEIGHT; y++)
    {
    	for(int x = 0; x < BOARD_WIDTH; x++)
    	{
    		//Do a switch on the value of the current index
    		// Since we are using a 1D array, we use the equation:
    		// x + y * BOARD_WIDTH  - This allows us to treat it like a 2D array
    		switch( g_BoardArray[ screen[1]+y+TEST ][ x + screen[0] ] )
    		{
    			case W01:
    				g_bmpTiles[x][y] = LoadABitmap("WA01.bmp");
    				break;
    			case W02:
    				g_bmpTiles[x][y] = LoadABitmap("WA02.bmp");
    				break;
    			case F01:
    				g_bmpTiles[x][y] = LoadABitmap("FL01.bmp");
    				break;
    		}
    	}
    }
    This code I run once, I found that out the hardway (I'm using gametutorials tilemaze and modifying it to scroll in-case you wanted to know. In WinMain():
    Code:
    for(int y = 0; y < BOARD_HEIGHT; y++)
    {
    	for(int x = 0; x < BOARD_WIDTH; x++)
    	{
    		// Display each title according to their index.
    		// We take the x and y index and times it by the width and height
    		// in pixels to get the tile position. This makes the nice tile effect we see on screen  
    		DisplayBitmap(&g_Buffer, g_bmpTiles[x][y], TILE_WIDTH * x, TILE_WIDTH * y);  
    	}
    }
    Now this works all fine and dandy IF you want the images to stay the same, (remember how init was run once?) anyway, to be able to scroll I put that loop from Init() into winmain (right before the DisplayBitmap loop, but this caused crashing, so I merged the two loops into one:
    Code:
    for(int y = 0; y < BOARD_HEIGHT; y++)
    {
    	for(int x = 0; x < BOARD_WIDTH; x++)
    	{
    		g_bmpTiles[x][y] = 0;
    		//Do a switch on the value of the current index
    		// Since we are using a 1D array, we use the equation:
    		// x + y * BOARD_WIDTH  - This allows us to treat it like a 2D array
    		
    		switch( g_BoardArray[ screen[1]+y ][ x + screen[0] ] )
    		{
    			case W01:
    				DisplayBitmap(&g_Buffer, LoadABitmap("WA01.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
    				break;
    			case W02:
    				DisplayBitmap(&g_Buffer, LoadABitmap("WA02.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
    				break;
    			case F01:
    				DisplayBitmap(&g_Buffer, LoadABitmap("FL01.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
    				break;
    		} 
    	}
    }
    But this still caused errors, note: all three arrays screen[], g_BoardArray[] and g_bmpTiles[] are global variables.
    Last edited by Dark Nemesis; 01-01-2004 at 12:22 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since your comment
    // x + y * BOARD_WIDTH - This allows us to treat it like a 2D array
    and your code
    switch( g_BoardArray[ screen[1]+y ][ x + screen[0] ] )
    are incompatible, you need to post some declarations.

    > DisplayBitmap(&g_Buffer, LoadABitmap("WA01.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
    My guess is LoadABitmap() returns a result which you need to 'free' at some point, so as it stands it is just a huge memory leak.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110
    Edit: I've found out what it is, thaks for your help, the problem was I was loading so many bitmaps but never freeing them. [/edit]

    g_BoardArray was originally a 1D array, I forgot to update the comment. Sorry for not fuilly explainning somethings.
    Code:
    // This is the number of X tiles for our board
    #define BOARD_WIDTH  15
    // This is the number of the Y tiles for our board
    #define BOARD_HEIGHT 12
    
    // This is the width in pixels of each tile
    #define TILE_WIDTH		32
    // This is the height in pixels of each tile
    #define TILE_HEIGHT		32
    
    HBITMAP g_bmpTiles[BOARD_WIDTH][BOARD_HEIGHT];
    
    int g_BoardArray[][15] =  {
    		{F01, F01, W01, W01, W01, F01, W02, W02, W02, W02, W02, W02, W02, W02, W02},
    		{F01, F01, F01, F01, F01, F01, W02, W02, W02, W02, W02, W02, W02, W02, W02},
    		{W01, F01, W01, W01, W01, F01, W02, W02, F01, W02, W02, W02, W02, W02, W02},
    		{W01, F01, W01, F01, W01, F01, F01, F01, F01, F01, F01, W02, W02, W02, W02},
    		{W01, F01, W01, F01, W01, F01, F01, W01, F01, W01, F01, F01, W02, W02, W02},
    
    		{F01, F01, F01, F01, W01, W01, W01, W01, W01, W01, F01, F01, W02, W02, W02},
    		{W01, F01, F01, F01, F01, W01, W01, W01, W01, F01, F01, W02, W02, W02, W02},
    		{W01, F01, F01, F01, F01, F01, W01, W01, W01, F01, W02, W02, W02, W02, W02},
    		{W01, W01, F01, W01, F01, F01, F01, W01, W01, F01, F01, W02, W02, W02, W02},
    		{W01, W01, W01, W01, F01, W01, F01, W01, W01, W01, F01, F01, W02, W02, W02},
    
    		{W01, W01, W01, W01, F01, W01, F01, W01, W01, W01, F01, W02, W02, W02, W02},
    		{W01, W01, F01, F01, F01, W01, F01, F01, W01, W01, F01, W02, W02, W02, W02},
    		{W01, W01, F01, F01, F01, W01, F01, F01, W01, W01, F01, W02, W02, W02, W02},
    		{W01, W01, F01, F01, F01, W01, F01, F01, W01, W01, F01, W02, W02, W02, W02},
    		{W01, F01, F01, F01, F01, W01, W01, F01, F01, W01, F01, F01, F01, W02, W02}}; // The end of the maze
    Here is the LoadBitmap Function:
    Code:
    HBITMAP LoadABitmap(LPSTR szFileName)
    {
    	// Load the bitmap and return the handle to the bitmap we just loaded
    	return (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    }
    
    void DisplayBitmap(BUFFER *pBuffer, HBITMAP hBitmap, int x, int y)
    {
    	// Select the bitmap handle into the extra hdc that holds the bitmap
    	HBITMAP hOldBitmap = (HBITMAP) SelectObject(pBuffer->hdcBitmap, hBitmap);
    
    	// Blit the bitmap hdc into the backbuffer
    	BitBlt(pBuffer->hdcBack, x, y, pBuffer->scrnRect.right, 
    		   pBuffer->scrnRect.bottom,  pBuffer->hdcBitmap, 0, 0, SRCCOPY);
    
    	// Select the old handle to the bitmap back where it was to clean up
    	SelectObject(pBuffer->hdcBitmap, hOldBitmap);
    }
    Last edited by Dark Nemesis; 01-01-2004 at 03:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char vs int - program crash!!
    By Goldrak in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2006, 08:17 PM
  2. My program causes my compiler to crash
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2006, 04:06 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM