Skip to main content
GameDev.net gamedev.net
🔒 Locked

Vertex Dynamic Memory (malloc and free)

Started by DavidJr Jan 12, 2016 at 10:01 AM 12 replies 6.5k views
Original Post
DavidJr
DavidJr

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

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



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

}
j_uk_dev
j_uk_dev

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.

AxeGuywithanAxe
AxeGuywithanAxe

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

j_uk_dev
j_uk_dev

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

Khatharr
Khatharr

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

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?

Dave Hunt
Dave Hunt




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.

DavidJr
DavidJr

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.

memory allocation


pMesh->pName = (char*)malloc(sizeof (char) * 33);
fread(pMesh->pName,...);
pMesh->pName[32] = '\0';
Dave Hunt
Dave Hunt

I was referring to the "..." in the fread call as well.

However, in your struct you declared cName as an array of 32 characters, then you likely attempted to set cName[32] to '\0'. Valid indices are 0-31. 32 is right out. It worked in your malloc version because you malloced 33 characters.

DavidJr
DavidJr

Dave Hunt, sorry for really late reply. Got so busy this week.

When I used array, I didn't set cName[32] to '\0'. Here is the way I call fread using array.


fread(cName, sizeof (char), 32, pIn);

Definitely my array was out of bound, because I only had 32. Recently, I tried to change the array size to 33. Still crashed.

Using pointer instead of array works fine. Perhaps I'm wrong?

Thank you very much so far, Dave Hunt.

Dave Hunt
Dave Hunt

If it works with the allocated pointer and not with the array, then you're doing something wrong. ;-)

We'd need to see your actual function to say for sure what's wrong.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.