Beginner Lesson 22 - ( homework )

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
natox1986
Posts: 53
Joined: December 14th, 2012, 1:11 pm

Beginner Lesson 22 - ( homework )

Post by natox1986 » January 3rd, 2013, 3:06 pm

Greetings fellow program-apprentices,
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 );
}
The "FreeSprite function" is neatly called in the Game destructor so this is all fine.

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 );
}
They're prototyped the right way in D3DGraphics.h and the "FreeFont function" is yet again, neatly defined in the Game destructor.

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 );
}
Okay, seems fine to me, right? Now comes the tricky part.
Debugging this code goes without any errors (Debug mode / Win32) but as I try to run the program it comes with the following error:
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.
Now if you ask me, this must be a problem with free'ing the allocated memory, but I cannot seem to find the problem.
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
Image

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Beginner Lesson 22 - ( homework )

Post by LuX » January 3rd, 2013, 3:34 pm

Those heap corruptions are always tricky ones to find since they don't show up as an error and if a problem comes up the compiler can't tell from where it comes.

When I run it and close debug or release, no corruption shows up. Usually this happens if you try to free memory that is already freed. Maybe your compiler makes the program destruct in a different order so that when it had automatically freed the memory you try to free it again in the game destruction.

In your non working version the problem is the size you allocate for the font. Take a look at it.

PS. I am dissapoint, son. you did not clear the projects before uploading.
ʕ •ᴥ•ʔ

User avatar
natox1986
Posts: 53
Joined: December 14th, 2012, 1:11 pm

Re: Beginner Lesson 22 - ( homework )

Post by natox1986 » January 3rd, 2013, 4:28 pm

In your non working version the problem is the size you allocate for the font. Take a look at it.
You, my friend, are a goddamn hero! You did not just give me the answer, but you made me THINK of my own solution! This was a great learning experience and I have fixed it!

Behold, my solution:

Code: Select all

******INSIDE GAME.CPP

// LOADING FONT -- CHANGE STRING VALUE HERE TO OTHER FONT --
LoadFont( &fixedSys,"Terminal5x13.bmp",5,13,160,39,32 );


*****INSIDE D3DGRAPHICS.CPP

// LOAD FONT INTO ALLOCATED MEMORY
void LoadFont( Font* font, const char* filename, int charWidth, int charHeight, int totalWidth, int totalHeight, int nCharsPerRow )
{
	font->surface = (D3DCOLOR*) malloc( sizeof(D3DCOLOR) * totalWidth * totalHeight );
	LoadBmp( filename,font->surface );
	font->charHeight = charHeight;
	font->charWidth = charWidth;
	font->totalHeight = totalHeight;
	font->totalWidth = totalWidth;
	font->nCharsPerRow = nCharsPerRow;

}


*****INSIDE D3DGRAPHICS.H

struct Font
{
	int charWidth;
	int charHeight;
	int nCharsPerRow;
	int totalWidth;
	int totalHeight;
	D3DCOLOR* surface;
};
void LoadFont( Font* font, const char* filename, int charWidth, int charHeight, int totalWidth, int totalHeight, int nCharsPerRow );

I gave the "LoadFont function" an additional 2 parameters to define the actual dimensions of the font bitmap. These parameters are called "totalHeight" and "totalWidth" and are defined in the structure. The definitions in the function point to the structure variables. And now it loads the complete bitmap font file into memory dynamically.

PS. I am dissapoint, son. you did not clear the projects before uploading.
I could have cleaned it, but I uploaded them to my own webspace so it doesn't fill up the PlanetChili.net webserver. And to be honest, I didn't really think about it either.
Image

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Beginner Lesson 22 - ( homework )

Post by LuX » January 3rd, 2013, 4:38 pm

Nice work.

I would have done it "cWidth * cPerRow * cHeight * 3" and saved the extra writing, but both work.

Not only would cleaning the project save space, it saves on download time too + if it's cleaned the compiler is forced to recompile a project that will certainly work, but meh.
ʕ •ᴥ•ʔ

Post Reply