This is an infuriating problem. The new operator is returning a null pointer. MSDN says it only doest that when there is insufficient memory, but I don’t see how that’s even remotely possible. For one thing its very early in my program and second when I check in task manager its only a bout 1MB used. What could possibly be causing this?
I'll give you some code.
class definition, this is the class I'm trying to make a new one of.
class OctTree
{
OctTree *Parent;
OctTree *Child[8];
AABB B;
unsigned int NumTris;
unsigned int *index;
vector3d* verts;
public:
OctTree()
{
NumTris = 0;
Parent = NULL;
for(int i = 0; i < 8; i++)
{
Child = NULL;
}
}
~OctTree()
{
for(int i = 0; i < 8; i++)
{
if(Child) delete Child;
Child = NULL;
}
if(Parent != NULL)
delete []index;
index = NULL;
}
void add_gemotry(vector3d* V, unsigned int num_verts, unsigned int *I,unsigned int num_tris);
// other methods...
};
This is the method where new is called and returns NULL
void OctTree::add_gemotry(vector3d* V, unsigned int num_verts, unsigned int *I, unsigned int num_tris)
{
verts = V;
index = I;
NumTris = num_tris;
num_tris = 0;
unsigned int *new_I = NULL;
if(Parent == NULL)
{
B.add_points(V,num_verts);
}
if(NumTris > 1)
{
for(int i = 0; i < 8; i++)
{
AABB Box = B.eighth(i);
for(unsigned int j = 0; j < NumTris; j++)
{
if( Box.contains(V[I[j*3]]) || Box.contains(V[I[j*3+1]]) || Box.contains(V[I[j*3+2]]))
{
num_tris++;
}
}
for(unsigned int j = 0, k = 0; j < NumTris; j++)
{
if( Box.contains(V[I[j*3]]) || Box.contains(V[I[j*3+1]]) || Box.contains(V[I[j*3+2]]))
{
if(new_I == NULL) new_I = new unsigned int[num_tris];
new_I[k*3] = I[j*3]; new_I[k*3+1] = I[j*3+1]; new_I[k*3+2] = I[j*3+2];
k++;
}
}
if(num_tris > 0)
{
if(Child == NULL)
{Child = new OctTree;} // This is the culprit, it fails on the very first time through, so I know I'm not accidentally recursively creating a huge tree.
Child->B = Box;
Child->Parent = this;
Child->add_gemotry(V, num_verts, new_I, num_tris);
}
num_tris = 0;
}
}
}
Oddly it works here.
class static_object :public object
{
OctTree *Tree;
public:
static_object()
{
Tree = new OctTree; // this works however
}
int LoadMS3DFile(char FileName[81])
{
if(Mesh::LoadMS3DFile(FileName))
{
Tree->add_gemotry(verts,num_verts,tris,num_tris);
}
else return 0;
return 1;
}
vector3d colision_point(object &O);
vector3d colision_normal(object &O);
vector3d colision_normal(vector3d &V);
};