#Weird memory corruption in C

14 messages · Page 1 of 1 (latest)

sonic herald
#

And this is how I initialize the mesh per chunk:

void chunk_init_meshes(Chunk* chunk)
{
    if (chunk == NULL) return;

    chunk->wallMesh = (Mesh){ 0 };
    chunk->blockMesh = (Mesh){ 0 };

    chunk->wallMesh.triangleCount = chunk->blockMesh.triangleCount = CHUNK_AREA * 2;
    chunk->wallMesh.vertexCount = chunk->wallMesh.triangleCount * 3;
    chunk->blockMesh.vertexCount = chunk->blockMesh.triangleCount * 3;

    chunk->wallMesh.vertices = (float*)malloc(CHUNK_VERTEX_COUNT * 3 * sizeof(float));
    chunk->blockMesh.vertices = (float*)malloc(CHUNK_VERTEX_COUNT * 3 * sizeof(float));

    chunk->wallMesh.texcoords = (float*)malloc(CHUNK_VERTEX_COUNT * 2 * sizeof(float));
    chunk->blockMesh.texcoords = (float*)malloc(CHUNK_VERTEX_COUNT * 2 * sizeof(float));

    chunk->wallMesh.colors = (unsigned char*)malloc(CHUNK_VERTEX_COUNT * 4 * sizeof(unsigned char));
    chunk->blockMesh.colors = (unsigned char*)malloc(CHUNK_VERTEX_COUNT * 4 * sizeof(unsigned char));

    UploadMesh(&chunk->wallMesh, true);
    UploadMesh(&chunk->blockMesh, true);
}

But this is where things get weird. You see, when I initialize the meshes, the meshes' values are fine, until it gets to chunk_genmesh(). For some reason, in there, all the values simply gets corrupted. I even removed everything inside that function and putting just a single print statement:

void chunk_genmesh(Chunk* chunk) {
    if (chunk == NULL) return;

    printf("After genmesh: %d\n", chunk->blockMesh.vertexCount);
}

And this is what gets printed:

Before upload: 0
INFO: VAO: [ID 30] Mesh uploaded successfully to VRAM (GPU)
INFO: VAO: [ID 31] Mesh uploaded successfully to VRAM (GPU)
After upload: 1536
After genmesh: -858993460

For reference, 1536 is the total number of Vertices and their position components. This is the correct value.

granite wing
#

step through it in a debugger, set a data breakpoint and have it break when the data changes.

#

why does chunk_genmesh(&chunks[1]); alway use chunk 1?

sonic herald
#

that was just for debugging, to see if it would work at least in a single chunk instead of all of them.

granite wing
#

I would use your debugger, you can set a break when any data address changes

sonic herald
#

okay wait

#

thinking better about it

#

its something on the chunk_manager_relocate function doing something

granite wing
#

chunks is allraedy a pointer, you shoujld not need to defrerence it

#

you can do chunks,2 to see it as an array in the debugger

sonic herald
#

yep, nevermind

#

i found the issue

#

i forgot to initialize the meshes for the newly generated chunks