Vertex Dynamic Memory (malloc and free)

Started by
12 comments, last by Dave Hunt 8 years, 2 months ago

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

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

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);

}

Run it under debugger and step through your code. Otherwise it's just guessing. There may be number of things going wrong including corrupted input data.

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

Also sounds weird that index would be an fVec2. It usually is an uint16 or uint32

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 ).

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?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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?


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.

This topic is closed to new replies.

Advertisement