Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

wolf_10

CopyConstructor

This topic is 5551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have a Question about my CopyConstructor, anyone knows why that dont work ???? The Program crasged when it''s call the Destructor CMSModel(const CMSModel &par) { m_position=par.m_position; m_timedelta=par.m_timedelta; m_velocity=par.m_velocity; m_speed=par.m_speed; m_numMeshes=par.m_numMeshes; m_pMeshes=new Mesh[m_numMeshes]; m_pMeshes=par.m_pMeshes; m_numMaterials=par.m_numMaterials; m_pMaterials=new Material[m_numMaterials]; m_pMaterials=par.m_pMaterials; m_numTriangles=par.m_numTriangles; m_pTriangles=new Triangle[m_numTriangles]; m_pTriangles=par.m_pTriangles; m_numVertices=par.m_numVertices; m_pVertices= new Vertex[m_numVertices]; m_pVertices=par.m_pVertices; Texture=par.Texture; } CMSModel *Model=new CMSModel(); Here is the Code to Copy CMSModel *newModel=new CMSModel(*Model); wolf

Share this post


Link to post
Share on other sites
Advertisement
You aren''t copying the arrays properly; use memcpy, not the assignment operator. What your code currently does is copy over the pointer, not the array.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
There's quite a few new's in there, got all the appropriate deletes?
Edit: Yup, Sneftel is right.

[edited by - Wildfire on August 11, 2003 9:42:20 AM]

Share this post


Link to post
Share on other sites

The problem is your allocations and assignations lines, like :

m_pMeshes=new Mesh[m_numMeshes];
m_pMeshes=par.m_pMeshes;

I''m sure that in your destructor you call: delete [] m_pMeshes,
and so you''ll delete multiple time the same memory area ...

What you really want to do in your constructor is probably this :

m_pMeshes=new Mesh[m_numMeshes];
for(int m=0; m < m_numMeshes ;++m)
{
m_pMeshes[m] = par.m_pMeshes[m]
}

Do the same thing for Materials, Triangles and vertices

Share this post


Link to post
Share on other sites
I have try this befor

memcpy(m_pVertices, par.m_pVertices, sizeof(Vertex));

but this dosent works too :-(

My Destructor:
.
.
m_numMeshes = 0;
if ( m_pMeshes != NULL )
{
delete[] m_pMeshes;
m_pMeshes = NULL;
}
.
.
.

Share this post


Link to post
Share on other sites
quote:

memcpy(m_pVertices, par.m_pVertices, sizeof(Vertex));


shouldn't that be

memcpy(m_pVertices, par.m_pVertices, par.m_numVertices *sizeof(Vertex));

You want to copy all of the vertices, not just a single one?

edit: Checking the pointer for null, and setting the pointer/counter to null/0 in the destructor is pretty much unneeded. As soon the the object is destroyed no one has access to those anymore.

[edited by - Wildfire on August 11, 2003 9:56:14 AM]

Share this post


Link to post
Share on other sites
Ok,
the Problem is here

glBegin( GL_TRIANGLES );
{
for ( int j = 0; j < m_pMeshes.m_numTriangles; j++ )
{
int triangleIndex = m_pMeshes[i].m_pTriangleIndices[j];

const Triangle* pTri = &m_pTriangles[triangleIndex];

for ( int k = 0; k < 3; k++ )
{
--------->int index = pTri->m_vertexIndices[k];<-------------
glNormal3fv( pTri->m_vertexNormals[k] );
glTexCoord2f( pTri->m_s[k], pTri->m_t[k] );
glVertex3fv( m_pVertices[index].m_location );
}
}
}
glEnd();

with the Copyconstructor
''index'' have a wrong Value

:-((

Share this post


Link to post
Share on other sites
did you first assign the memory before calling memcopy? I agree with the rest of them that your problem is here. In the destructor, it doesn''t matter calling delete on NULL it will not crash your program, but calling delete on memory that has already been freed will crash.
If you don''t like memcopy, use the for loop as suggested by Sphax.

Share this post


Link to post
Share on other sites
Wildfire,
yes your right, but this dosent work

m_pMeshes=new Mesh[m_numMeshes];
memcpy(m_pMeshes, par.m_pMeshes, par._numMeshes*sizeof(Mesh));


Share this post


Link to post
Share on other sites
desertcube,

that''s exactly is my Problem, the Destructor try delete Memory that dosent exist.
But i dont no how avoid that.
I belief the Copyconstructor works wrong, thats the Problem.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!