Game Development Community

Special considerations when using the "new" operator?

by Brett Fattori · in Torque Game Engine · 01/08/2004 (6:02 am) · 8 replies

I have been trying, now, for the past week or so to build up edgelists for stencil shadowing. Many times I've redesigned them: first as a list manager, second on TSShape, and finally on TSMesh. My class is relatively simple (this is from memory, so any syntax errors -- please be lenient):
struct SVShadowEdge
{
   U32 vertices[2];
   U32 triangles[2];
};

class SVEdgeList
{
   public:
      Vector<SVShadowEdge> mList;
      TSMesh* mesh;
      bool listActive;

   public:
      SVEdgeList();
      ~SVEdgeList();

      void RebuildEdgeList(TSMesh* meshObj);
};

Within TSMesh, I have created a simple pointer to a SVEdgeList called edgeList:
class TSMesh
{
   ...

   SVEdgeList* edgeList
};

This holds a ref to a new object that will be created in TSMesh::assembleMesh(). There is a call in that method to the following function. I added a method to TSMesh to call the edgelist builder (this is from memory too, so forgive me for syntax):
void TSMesh::buildMeshEdges()
{
   if (!edgeList)
   {
      // There is a determination of whether this is
      // a good mesh (Standard or Skin) but I cannot
      // remember the exact code...  It goes into
      // "badMesh"

      if ((indicies.size() > 0) && !badMesh)
      {
         edgeList = new SVEdgeList();
         edgeList->RebuildEdgeList(this);
      } else {
         edgeList = NULL;
      }
   }
}

The problem I'm having is that it works fine for about 3 to 4 meshes. On the fifth call, when it hits the call to new I get an access violation. The violation occurs in PlatformMemory.cc in the treeRemove method once it has determined that there is a header. It then tries to assign the nextQueue and prevQueue, but what I see is that prev and next refer to 0x00000007 and 0x00000013. It's the same each time and it's blowing up at this point.

I've tried disabling the TGE memory manager, turned on DEBUG_GUARD, and examined numerous other classes that do something similar. For instance, ShapeBase has a pointer to the Shadow class, which it creates on the fly with a call to "mShadow = new Shadow()". It doesn't seem that I'm doing anything odd here.

Any ideas?

- Brett

#1
01/08/2004 (6:13 am)
I think you and I may have a similar problem and I *think* it is being caused by Vector<>. I have seen some real nasty errors when I nest a Vector<> inside a class that gets used in another Vector<> (I end up with bad memory pointers like crazy). Not exactly the same bug but they may be related?
#2
01/08/2004 (8:46 am)
I have noticed that the Vector type has a note that specifically says something about not creating/destructing objects within. Maybe that has something to do with it. However, it is just a simple vector to a struct.

- Brett
#3
01/08/2004 (11:20 am)
Yeah, Phil mentioned the creation/destruction thing to me yesterday. My code was 2 small structs. I unnested my Vector's today and it worked fine. Have you tried a test w/o the Vector? Also, I vaguely remember "fixing" a bug like this with a full recompile once.
#4
01/08/2004 (11:48 am)
I have had very very bad experiences using vector<> classes for anything except manipulating raw values... :-/
#5
01/08/2004 (12:49 pm)
I was going to try using a pointer to pointer of struct, but I'm still brushing off my C/C++ skills. Things are a tad foggy still...

@Matthew: What do you mean by "unnested my Vector's"?

- Brett
#6
01/08/2004 (1:20 pm)
A Vector is not supposed to contain any type that has a constructor call in it. Which means that you shouldn't contain types that have members which are Vectors, as Vectors themselves have constructors.
#7
04/09/2007 (1:45 pm)
Quote:
Also, I vaguely remember "fixing" a bug like this with a full recompile once.

YES! That was it for me. I was trying to add more to the Move struct and kept hitting a debug break in checkGuard(). I tried stock TGE and TSE, then scoured the forums for a while and found this. In the process I also got to learn some about Torque's memory manager, which was nice.

When I did a full rebuild, the bug was "fixed."

Thanks a lot!
#8
04/11/2007 (11:00 am)
What does your SVEdgeList constructor look like?

Also, have you tried "#include " and using that instead?