I would like to request your aid to solve an issue that has been bugging me for a while.
The homework assignment in beginner lesson 22 demands that I rebuild the "LoadFont function" to make it use Dynamic Memory Allocation.
Now this should be rather simple because it has been completely explained to us using the "LoadSprite function".
** A little side-note: My program ran perfectly before I started editing the LoadFont function. **
The "LoadSprite function" and the "FreeSprite function" are working perfectly as following:
Code: Select all
/**************/
D3DGraphics.cpp
/**************/
// LOAD SPRITE INTO ALLOCATED MEMORY
void LoadSprite( Sprite* sprite, const char* filename, unsigned int width, unsigned int height, D3DCOLOR key )
{
sprite->surface = ( D3DCOLOR*) malloc( sizeof(D3DCOLOR ) * width * height );
LoadBmp( filename,sprite->surface );
sprite->height = height;
sprite->width = width;
sprite->key = key;
}
// FREE UP ALLOCATED MEMORY
void FreeSprite( Sprite* sprite )
{
free( sprite->surface );
}
Now I modified the "LoadFont function" and I created a new function called the "FreeFont function" like this:
Code: Select all
/**************/
D3DGraphics.cpp
/**************/
// LOAD FONT INTO ALLOCATED MEMORY
void LoadFont( Font* font, const char* filename, int charWidth, int charHeight, int nCharsPerRow )
{
font->surface = (D3DCOLOR*) malloc( sizeof(D3DCOLOR) * charWidth * charHeight );
LoadBmp( filename,font->surface );
font->charHeight = charHeight;
font->charWidth = charWidth;
font->nCharsPerRow = nCharsPerRow;
}
// FREE UP ALLOCATED MEMORY (FONT)
void FreeFont( Font* font )
{
free( font->surface );
}
Code: Select all
/**************/
D3DGraphics.h
/**************/
void LoadFont( Font* font, const char* filename, int charWidth, int charHeight, int nCharsPerRow );
void FreeFont( Font* font );
Code: Select all
/**************/
Game.cpp
/**************/
// GAME DESTRUCTOR (kill /initial/ loads)
Game::~Game()
{
FreeSprite( &backgroundSprite );
FreeAnimatedSprite( &dudeTemplate );
FreeFont( &fixedSys );
}
Debugging this code goes without any errors (Debug mode / Win32) but as I try to run the program it comes with the following error:
Now if you ask me, this must be a problem with free'ing the allocated memory, but I cannot seem to find the problem.Windows has triggered a breakpoint in Chili DirectX Framework.exe.
This may be due to a corruption of the heap, which indicates a bug in Chili DirectX Framework.exe or any of the DLLs it has loaded.
This may also be due the user pressing F12 while Chili DirectX Framework.exe has focus.
The output window may have more diagnostic information.
Is there anyone that can explain to me what is going wrong? (or give me a hint into the right direction).
FRAMEWORK SOLUTIONS // Original (working) && Edited (not working)
Working Version: http://www.gamer-bay.com/download/Font% ... rking).rar
Non Working Version (edited): http://www.gamer-bay.com/download/Edite ... rking).rar