Game Development Community

FrameAllocator::alloc bug

by William Todd Scott · in Torque Game Engine Advanced · 09/18/2007 (7:59 pm) · 2 replies

Hi,

I there appears to be a bug in the FrameAllocator::alloc function if the FRAMEALLOCATOR_DEBUG_GUARD define is set.

Here is the function:

void* FrameAllocator::alloc(const U32 allocSize)
{
   U32 _allocSize = allocSize;
#if defined(FRAMEALLOCATOR_DEBUG_GUARD)
   _allocSize+=4;
#endif
   AssertFatal(smBuffer != NULL, "Error, no buffer!");
   AssertFatal(smWaterMark + allocSize <= smHighWaterMark, "Error alloc too large, increase frame size!");

   U8* p = &smBuffer[smWaterMark];
   smWaterMark += allocSize;

#if defined(TORQUE_DEBUG)
   if (smWaterMark > sgMaxFrameAllocation)
      sgMaxFrameAllocation = smWaterMark;
#endif

#if defined(FRAMEALLOCATOR_DEBUG_GUARD)
   U32 *flag = (U32*) &smBuffer[smWaterMark-4];
   *flag = 0xdeadbeef ^ smWaterMark;
#endif
   return p;
}

Notice that the _allocSize variable is never actually used. It is assigned to but not used in the actual allocation.

I'm not sure if the fix is to change the line:

smWaterMark += allocSize;

to

smWaterMark += _allocSize;

Or, if the FRAMEALLOCATOR_DEBUG_GUARD define isn't fully functionaly and should not be used.

Todd

#1
09/18/2007 (9:03 pm)
Nice catch! That's been fixed in TGE, it just didn't make its way over to TGEA...

The fix in TGE is to use _allocSize throughout.

Fixed in the repo - thanks!
#2
09/18/2007 (9:55 pm)
Cool.

I was being a bit lazy not tracking down the solution myself, so thanks for the help!

Todd