Game Development Community

Bug in GFXTextureManager::hashFind

by Jon Wilsdon · in Torque Game Engine Advanced · 01/27/2006 (2:55 pm) · 0 replies

My understanding of the hashFind function is that it takes in a string describing the texture, hashes it and then if there are any collisions it walks through all of the other strings that hashed to the same thing (doing string compares) and then returns the texture object pointer that matches the string.

The problem we have run across is that GFXTextureManager::createTexture takes in a bitmap, and checks to see if the bitmap has a source resource somewhere and if it doesn't then it sets the filename to be NULL and calls hashFind on it to see if it already exists in the cache. But hashFind doesn't handle the NULL value that is passed in properly. StringTable->hashString() returns -1 which then gets modded by mHashCount which will end up returning the last entry in the hash table. This works most of the time, but when there are lots of textures in being managed then the hash table fills up and all of a sudden the engine tries to create a new texture and you get a string comparison between a filename and a NULL value and strcmp() likes to crash things when it is given a NULL value.

The solution we are using is to add a check for a NULL filename at the top of hashFind() and simply return NULL (that is what happens before the last hash slot is taken anyway) if the name being passed in is NULL.

if( !name ) return NULL;

This has gotten rid of some hard-to-pin-down crashes when entering the editor and some crashes that happen while running through the world.

If for some reason my understanding of this problem is incorrect, I'd love to have someone let me know! Or if this has been fixed already and I just missed it, let me know.

In any event, hopefully it helps someone out since I didn't find anything after doing a quick search on it.