Jump to content
  • Advertisement
Sign in to follow this  
DavidJr

Vertex Dynamic Memory (malloc and free)

This topic is 900 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, when I try to read mesh file, I got error on the second loop, especially when the parser calls malloc for the second time. When I try different model with more mesh, e.g: 4 mesh, in mesh loop on count 3 it crashed. The VS tells me it can be heap corruption and there's "Critical error detected c0000374" in the output.

Here is the code:

for (int i = 0; i < iMesh; ++i) {
   Mesh *pMesh = &pModel->pMesh[i];
   fread(&pMesh->iTr, sizeof (int), 1, pIn);

  pVe = (fVec3*)malloc(sizeof (fVec3) * pMesh->iTr);
  pVn = (fVec3*)malloc(sizeof (fVec3) * pMesh->iTr);
  pVt = (fVec2*)malloc(sizeof (fVec2) * pMesh->iTr);
  pIndex = (fVec2*)malloc(sizeof (fVec2) * pMesh->iTr);

  for (int j = 0; j < pMesh->iTr; ++j) {
  //fread etc.
  }

  // OpenGL stuffs, buffer data

  free(pVe);
  free(pVt);
  free(pVn);
}

Is this memory leak? What did I do wrong? Please help, I'm really struck here sad.png

I appreciate any reply, thanks before

Edited by DavidJr

Share this post


Link to post
Share on other sites
Advertisement

 

With fread,etc, do you cause a buffer overrun by copying more bytes than you allocated?

 

No, the buffer reads correctly IMO. Take a look :)

for (int j = 0; j < pMesh->iTr; ++j) {
     fread(&pMesh->pVe[j], sizeof (fVec3), 1, pIn);
     fread(&pMesh->pVn[j], sizeof (fVec3), 1, pIn);
     fread(&pMesh->pVt[j], sizeof (fVec2), 1, pIn);
     fread(&pMesh->pIndex[j].x, sizeof (int), 1, pIn);

}

Share this post


Link to post
Share on other sites

Well, if "pMesh->iTr" is what I think it means, which is the triangle count, it seems that you aren't allocating enough vertices. A triangle should have 3 vertices each, i.e 

pVertices = malloc(sizeof(fVec3) * pMesh->iTr * 3),

but you're allocating it as

pVertices = malloc(sizeof(fVec3)* pMesh->iTr )

Because of all of the possibilities that it could be, it would be helpful if you included the full code verbatim instead of chunks, just like j_uk_dev said, until then, it would just be us guessing

Edited by AThompson

Share this post


Link to post
Share on other sites

It looks also weird that number of indices is equal to number of vertices/normals/uvs. Although in the case when vertices can't be reused it's possible, but why to use the indices then? This is where the problem may occur. Indices should be probably serialised in the separate iteration. For example for the simplest cube you may have 8 vertices only, but 36 indices (6 sides, each 2 tris and each tri 3 vertices, I assume drawing triangles, not stripes ). Use debugger, see what values you read, find when it gets corrupted ( I'm pretty sure, before the crash, the data corruption will show if you investigate content of your buffers while reading ).

Edited by j_uk_dev

Share this post


Link to post
Share on other sites

Instead of doing all of these individual allocations and reads why not just create a structure that matches the format of the data in the file and read the whole thing in one call?

Share this post


Link to post
Share on other sites

Hello guys, I think I've found the problem. Yesterday I tried to comment those codes, the error appears when I tried to read the mesh name. I've char array in my dynamic struct, tried to change the char array to char pointer and allocate the char pointer fixed the problem.

 

e.g:

typedef struct Mesh
{
    char cName[32];
} Mesh;
 
fread(pMesh->cName, ....);

 

->

typedef struct Mesh
{
   char *pName;
} Mesh;
 
...allocating the char
fread(pMesh->pName,...);
...string terminate

 

Thank you very much for all of your help :)

Btw, I'm curious why this happened?

Share this post


Link to post
Share on other sites


Btw, I'm curious why this happened?

Very often the devil's in the ellipsis. It's hard to say what happened without knowing what "..." is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!