Original Post
Now this is something I'm finding really difficult to understand... :P I'm building an Octree class which recursively builds from the root to its nodes. I've single stepped through the code, and pinned down the problem to one place, and when it happens. This code is built of the tutorial found at GameTutorials.com The "SubDivideNode" method calls CreateNewNode to build the 8 nodes off the current one. The problem happens the first node of the root of the tree is recursively subdivided. Every thing seems to divide perfectly, all the nodes get set up nice... but when it comes back out to the root node as soon as it tries to "delete [] pvNodeVertices" the debugger suddenly spews out: "User breakpoint called at (Insert random memory adress here)" Which happens to be in the module of "ntdll.dll". This is also where the program seems to crash, my LogImmediate functions writes text to a file. CVector3 doesn't do any memory management of its own. And pvNodeVertices seems to always allocate memory without "throw"ing anything bad... What's going on ? :) Thanks for your time in advance.
void COctree::CreateNewNode(CVector3 *Vertices, vector<bool> TriangleList, unsigned int NumVertices, CVector3 Center, float Width, unsigned int TriCount, unsigned int NodeID)
{
int i;
int index = 0;
CVector3 *pvNodeVertices;
if(TriCount)
{
try {
pvNodeVertices = new CVector3 [TriCount * 3];
} catch (bad_alloc xa) {
LogImmediate("Log.txt", "Failed allocation");
}
++NodeCount;
//We're adding 3 (2?) vertices for every triangle found
for(i = 0; i < NumVertices; ++i)
{
if(TriangleList[i / 3])
{
pvNodeVertices[index] = Vertices;
index++;
}
}
m_pOctreeNodes[NodeID] = new COctree; //Creating a new Octree for this Node
m_pOctreeNodes[NodeID]->m_NodeID = (eOctreeNodes)NodeID;
// Get the new node's center point depending on the nodexIndex (which of the 8 subdivided cubes).
CVector3 vNodeCenter = GetNewNodeCenter(Center, m_Width, NodeID);
//Since it will start recurse starting here, increment the Subdivision count
++m_iSubDivisionCount;
//This will assign the vertices to the node if necessary so we can delet pvNodeVertices after.
LogImmediate("Log.txt", "Subdividing Current Node");
m_pOctreeNodes[NodeID]->SubDivideNode(pvNodeVertices, TriCount * 3, Width / 2, vNodeCenter); // The new width is HALF the previous width
//We've just come out of recursing so decrease the Subdivision count
LogImmediate("Log.txt", "Finished Subdividing Node");
--m_iSubDivisionCount;
delete [] pvNodeVertices;
LogImmediate("Log.txt", "Sucessful delete");
}
}