The curse of char*

Whew! Finally I was able to solve the bug I was tring to fix in my game. To explain briefly, I have an std::map<char*, BITMAP*> which is a static member of a class. Now, I have a Manager class which loads resources that somehow looked like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BITMAP* BitmapManager::addBitmap(char* key, char* fileName)
{
BITMAP* bitmap = load_bitmap(fileName, NULL);

bitmapTable.insert( std::pair<char*, BITMAP*>(key, bitmap) );

return bitmap;
}

BITMAP* BitmapManager::getBitmap(char* key)
{
std::map<char*, BITMAP*>::iterator it = bitmapTable.find(key);

BITMAP* bitmap = it->second;

return bitmap;
}


As my code goes deeper and deeper and I pass a reference to a BITMAP by calling the BitmapManager's getBitmap function, I somehow encountered a situation when the pointer already became unreacheable and my program would crash when I make a call to BITMAP's(bitmap is a struct) w member which is equivalent to the bitmap width . The call to getBitmap would return NULL or 0.

Thanks to some guy in the allegro.cc forums who advised me to use std::string instead of char*. I don't know the exact reason why this would happen, but the solu worked anyway.


In the end, I didn't use a char* or std::string at all and just used an enum type(implicit integer) for my map key. Big Smile


The final code look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BITMAP* BitmapManager::addBitmap(BitmapKeys key, char* fileName)
{
BITMAP* bitmap = load_bitmap(fileName, NULL);

bitmapTable.insert( std::pair<BitmapKeys, BITMAP*>(key, bitmap) );

return bitmap;
}

BITMAP* BitmapManager::getBitmap(BitmapKeys key)
{
std::map<BitmapKeys, BITMAP*>::iterator it = bitmapTable.find(key);

BITMAP* bitmap = it->second;

return bitmap;
}

Published 02-02-2007 2:33 AM by lamia
Filed under:

Comments

Sunday, February 04, 2007 5:55 PM by Orange

# re: The curse of char*

Wow. Maybe you can fix this for me. hehe. YM na lang kita mamaya.

AUX_RGBImageRec myGrapix::*LoadImage(char *FileName)

{

       FILE *File=NULL;                      

       if (!FileName)                    

               return NULL;                  

       File=fopen(FileName,"r");                  

       if (File)                                

       {

               fclose(File);                        

               return auxDIBImageLoad(FileName);    

       }

       return NULL;                              

}

Sunday, February 04, 2007 10:26 PM by lamia

# re: The curse of char*

What's wrong in your code Orange? And what is FileName?

Monday, February 05, 2007 12:11 PM by keithrull

# re: The curse of char*

i think he is trying to load File based on the parameter Filename

Monday, February 05, 2007 10:11 PM by lamia

# re: The curse of char*

Anyway, my first suggestion was for him to change his code from this

AUX_RGBImageRec myGrapix::*LoadImage(char *FileName)

to this

AUX_RGBImageRec* myGrapix::LoadImage(char *FileName)

As a guide, try to remember that it's

<TYPE>   <CLASS>::<MEMBER>

if it's a constructor then

<CLASS>::<MEMBER>