the new operator

Started by
11 comments, last by jLeslie 23 years, 11 months ago
Ok, I''ve created a few classes to hold data about my 3D objects, yadda yadda yadda......But I''m having trouble allocating memory within one of my classes. Here''s the declaration for the main object class which contains pointers to various meshes and skeletons and whatnot that make up the object for my game. class OBJECTCLASS { public: char Name[80]; int Type; int NumAnimations; int NumMeshs; MESHCLASS* cMesh[10]; ANIMATIONCLASS* cAnimations[15]; SKELETONCLASS* cSkeleton; OBJECTCLASS(); ~OBJECTCLASS(); HRESULT LoadObject(char* strFileName); HRESULT AddMesh(MESHCLASS* cMeshObject); HRESULT AddAnimation(ANIMATIONCLASS* cAnimationObject); HRESULT AddSkeleton(SKELETONCLASS* cSkeletonObject); HRESULT UpDateMe(LPDIRECT3DDEVICE7 D3DDevice); private: }; The problem is with the LoadObject function, which is listed here.....The first time through the loop it seems to work fine, but when it finds another mesh to load in the file and enters...if (!strcmp(Chunk_Name, "PolH"))...for the second time, I get an access violation when I try to allocate memory with the new statement for cMesh[...]. HRESULT OBJECTCLASS::LoadObject(char* strFileName) { FILE *myfile = fopen(strFileName, "r"); char Chunk_Name[80]; while((fscanf(myfile, "%s" , Chunk_Name)) != EOF) { if(!strcmp(Chunk_Name, "Bone")) { BONECLASS* cBoneObject; cBoneObject = new BONECLASS; cSkeleton->AddBone(cBoneObject); } if (!strcmp(Chunk_Name, "Join")) { JOINTCLASS* cJoint; cJoint = new JOINTCLASS; BONECLASS* cBone; cBone = new BONECLASS; cJoint = LoadJoint(myfile); cBone = cSkeleton->FindBone(cJoint->Bone1); cBone->AddJoint(cJoint); cBone = cSkeleton->FindBone(cJoint->Bone2); cBone->AddJoint(cJoint); } if (!strcmp(Chunk_Name, "PolH")) { cMesh[NumMeshs] = new MESHCLASS; cMesh[NumMeshs]->LoadMesh(myfile); NumMeshs++; } } return S_OK; }
Advertisement
you set num_meshs to zero before using the above function right?
___________________________Freeware development:ruinedsoft.com
Ya, in the OBJECTCLASS constructor I initialized NumMeshs to 0.
Don''t know if this helps any, but I played around with it some and found out I get another access violation if I try to delete the MESHCLASS pointer after I allocated memory for it with new.
just to clarify, if you call LoadObject() you should reset numMeshes to 0 each time. Not just in the constructor because if you call that twice in a row by accident you will add meshes to the array that you may not have room for.

try just doing the whole program with a single "mesh" and change
MESHCLASS * cMesh[10];
to
MESHCLASS * cMesh;

just to make sure this is working.
Call
if(cMesh){
delete cMesh;
cMesh = NULL;
}
at the end so you don''t delete something twice.

If you''re already dynamcally creating these meshes, why not just do a dynamic array of messages instead?
MESHCLASS * cMesh = new MESHCLASS[numMeshes];

(don''t for get to delete with brackets
if(cMesh){
delete[] cMesh;
cMesh = NULL;
}
___________________________Freeware development:ruinedsoft.com
First, think of the ObjectClass as the root object that stores the main transform matrix and any other properities I might need for the game, like density and various statistics for physics calculations; and the other 10 meshs as siblings, each capable of having pointers to seperate children and so on.
Second, when I first started making it I was going to use a dynamic array, but to simplify things at first I just used a normal array of 10 meshs.
I''ve been skimming through my C++ book, and I can''t find any reason why I shouldn''t be able to delete memory that I just allocated with new, if anyone knows of any cases where you can''t, let me know, it may help me in the future
honestly, a dynamic 1dim array is no harder than allocating a single variable.

the only time you can''t "delete" stuff is when it wasn''t created with "new" or it has already been deleted. Note that when using new[some value] you have to use "delete[]".

If you wanted to simplify the project why not try
MESHCLASS cmesh[10];
cmesh[nummeshes].Load();
instead of bothering with dynamically allocating data?

If you post some more source I might see the nature of the problem.
___________________________Freeware development:ruinedsoft.com
I messed around with it somemore and found the problem with the delete statement had something to do with my MESHCLASS destructor shown here...
MESHCLASS::~MESHCLASS()
{
if (Vertices)
delete Vertices;
if (Faces)
delete Faces;
for (int i = 0;i < NumChildren;i++)
{
if (Children[NumChildren])
delete Children[NumChildren];
}
}

Heres the constructor for the MESHCLASS, don''t know if that will help....

MESHCLASS::MESHCLASS()
{
Id = 0;Parent = 0;NumChildren = 0;
Center.x = 0.0f;Center.y = 0.0f;Center.z = 0.0f;
XAxis.x = 0.0f;XAxis.y = 0.0f;XAxis.z = 0.0f;
YAxis.x = 0.0f;YAxis.y = 0.0f;YAxis.z = 0.0f;
ZAxis.x = 0.0f;ZAxis.y = 0.0f;ZAxis.z = 0.0f;
Transform._12 = Transform._13 = Transform._14 = Transform._21 = Transform._23 = Transform._24 = 0.0f;
Transform._31 = Transform._32 = Transform._34 = Transform._41 = Transform._42 = Transform._43 = 0.0f;
Transform._11 = Transform._22 = Transform._33 = Transform._44 = 1.0f;
}

Here''s the constructor for the OBJECTCLASS...

OBJECTCLASS::OBJECTCLASS()
{
Type = 0;NumAnimations = 0;NumMeshs = 0;
cSkeleton = new SKELETONCLASS;

}

And here''s the declaration for them both atm....

class MESHCLASS
{
public:

float Id;
float Parent;

char Name[80];

char Texture[80];
D3DMATERIAL7 Material;
DWORD NumVertices;
D3DVERTEX* Vertices;
DWORD NumFaces;
WORD* Faces;

D3DVECTOR Center;
D3DVECTOR XAxis;
D3DVECTOR YAxis;
D3DVECTOR ZAxis;
D3DMATRIX Transform;

int NumChildren;
MESHCLASS* Children[10];

MESHCLASS();
~MESHCLASS();

HRESULT LoadMesh(FILE *myfile);
HRESULT AddChild(MESHCLASS* cChildMesh);
HRESULT AddSibling();
HRESULT Render(LPDIRECT3DDEVICE7 D3DDevice);

private:

};

class OBJECTCLASS
{
public:

char Name[80];
int Type;

int NumAnimations;
int NumMeshs;
MESHCLASS* cMesh[10];
ANIMATIONCLASS* cAnimations[15];
SKELETONCLASS* cSkeleton;


OBJECTCLASS();
~OBJECTCLASS();

HRESULT LoadObject(char* strFileName);
HRESULT AddMesh(MESHCLASS* cMeshObject);
HRESULT AddAnimation(ANIMATIONCLASS* cAnimationObject);
HRESULT AddSkeleton(SKELETONCLASS* cSkeletonObject);

HRESULT UpDateMe(LPDIRECT3DDEVICE7 D3DDevice);

private:
};

The LoadObject function for OBJECTCLASS is listed in my first post. I don''t think any of the other functions have anything to do with it.

I was thinking maybe it has something to do with the way I declared them or something, but I''m new to using classes and I don''t really have any idea right now, guess I''ll sit down and read somemore
I think your problem is that you don''t set all the pointers in your arrays to NULL in your constructor. You can''t guarantee that they are initialized to zero, so when your destructor is called, it tries to free non-allocated memory ... program goes BANG!!!

I think that''s it,

Let me know if it worked
I don''t think that is the problem because it only deletes up to NumChildren, so it never touches the ones I haven''t initialized yet.
I rewrote it some to match my bone loading function which does almost the same thing, only it works. Now I''m getting an access violation exception when I try to allocate memory for my array of Faces

This topic is closed to new replies.

Advertisement