Game Development Community

A Vector inside of a Vector

by Bullitt Sesariza · in Torque Game Engine · 05/24/2009 (9:37 pm) · 3 replies

Hi guys, I have a vector of, say, ClassA and in turn ClassA also has another vector of ClassB as its member variable. To instantiate both vectors, I use this routine:
for (int i = 0; i < itemCount; ++i )
	{
		mClassA.increment(); //or ClassB
		ClassA& tempA = mClassA.last(); //or ClassB

		/*The usual initialization for each item of the vector
		*/

		...
	}

The problem is in the second vector / vector of ClassB which is inside of each instance of ClassA, it would crash at this point:

bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize)
{
   if (newCount > 0)
   {
      U32 blocks = newCount / VectorBlockSize;
      if (newCount % VectorBlockSize)
         blocks++;
      S32 mem_size = blocks * VectorBlockSize * elemSize;
      *arrayPtr = *arrayPtr ? dRealloc(*arrayPtr,mem_size) :
         [b]dMalloc(mem_size);[/b] //This is where the debugger pointed the crash

      *aCount = newCount;
      *aSize = blocks * VectorBlockSize;
      return true;
   }

   if (*arrayPtr) 
   {
      dFree(*arrayPtr);
      *arrayPtr = 0;
   }

   *aSize = 0;
   *aCount = 0;
   return true;
}

The error message itself is: Unhandled exception at 0x0086b002 in torqueDemo_DEBUG.exe: 0xC0000005: Access violation reading location 0xcdcdcdc8.

How do I solve this? Thanks in advance.

PS: in case anybody asks this, yeah, I've already added the VECTOR_SET_ASSOCIATION keyword.

#1
05/26/2009 (9:23 am)
if i had to guess i would guess that arrayPtr is being passed in with garbage instead of a pointer to an array (or NULL). try looking back in the stack and seeing who calls VectorResize() and why arrayPtr is garbage.
perhaps increment() is trying to resize the member vector before properly allocating it ?
#2
06/01/2009 (9:54 pm)
You cannot use a class that requires construction/destruction in Torque's Vector container. Taken directly from the declaration of Vector:
/// <b>***WARNING***</b>
///
/// This template does not initialize, construct or destruct any of
/// it's elements.  This means don't use this template for elements
/// (classes) that need these operations.  This template is intended
/// to be used for simple structures that have no constructors or
/// destructors.

Your class requires construction because it contains a Vector member.
#3
06/03/2009 (1:21 am)
Sorry for the long delay.

@Orion: well, that one is from Vector's source code. and the one calling it was the "mClassB.increment();". maybe it's like what Wes said. the Vector can't process the constructor. thanks though.

@Wes: oic, that figures. didn't read it well before. thanks for the answer.

again, thanks guys.