0

I get a segmentation fault checking for if

if(chunk->nodeBlock->nextBlock == NULL)

on line 40, inside of createChunk. It seg faults after it's second run through the for loop, I checked to see if it was going inside of the if statement the first time around and it does. I also checked to see that chunk->nodeBlock->nextBlock has an existing address after the code block inside the if statement is ran, meaning it should go to the else statement on the second run through. However it seg faults as it checks the if statement the second time.

        #include "Render.h"
    #include "Block.h"
    struct Chunk
    {
        struct Render *r;
        struct Block *nodeBlock;
    };

    //struct Block *NewBlock = NULL;

    float posOffset = 1.0f;
    int blockCount = 0;
    struct Chunk *makeChunk(struct Render *r)
    {
        struct Chunk *chunk = NULL;
        chunk = malloc(sizeof(struct Chunk));
        chunk->r = r;

        chunk->nodeBlock = makeBlock(r->model->worldPosition);
        chunk->nodeBlock->nextBlock = NULL;

        return chunk;
    }

    //TODO: Change this so it doesn't take in the model (source of the node block) instead, take in a currentBlock block pointer. 
    void updateModelMatrix(struct Chunk *chunk)
    {
        makeModelMatrix(chunk->r->model,0,0,0,0);
    }

    void createChunk(struct Chunk *chunk)
    {
                vec3 nodeCoordinate;
                nodeCoordinate[0] = chunk->nodeBlock->pos[0];
                nodeCoordinate[1] = chunk->nodeBlock->pos[1];
                nodeCoordinate[2] = chunk->nodeBlock->pos[2];

        for(int x = 0; x<16; x++)
        {
                if(chunk->nodeBlock->nextBlock == NULL)
                { 
                    //only has the node block
                    blockCount++;// should be one now
                    nodeCoordinate[0] += posOffset * blockCount;
                    chunk->nodeBlock->nextBlock = makeBlock(nodeCoordinate);
                    //chunk->no
                }
                
                else
                { 
                    //get to the newest block.
                    while(chunk->nodeBlock->nextBlock != NULL)
                    {
                        chunk->nodeBlock = chunk->nodeBlock->nextBlock;
                    }
                    //after loop, we have reached the newest block
                    blockCount++;
                    nodeCoordinate[0] += posOffset * blockCount;
                    chunk->nodeBlock->nextBlock = makeBlock(nodeCoordinate);
                }
        }
    }


    void printChunk(struct Chunk *chunk)
    {
        struct Block *temp;

        temp = chunk->nodeBlock;

        if(temp == NULL)
        {
            printf("No blocks.");// should never print 
        }
        else
        {
            while(temp != NULL)
            {
                printf("BLOCK:\n");
                printVec3(temp->pos);
                temp = temp->nextBlock;
                printf("\n");
            }
        }
    }

Edit: here is Block.h in case this is causing the problem:

    struct Block
{
    vec3 pos;
    struct Block *nextBlock;
};

struct Block *makeBlock(vec3 pos)
{
    struct Block *block = malloc(sizeof(struct Block));

    block->pos[0] = pos[0];
    block->pos[1] = pos[1];
    block->pos[2] = pos[2];
    
    return block;
}
  • 5
    If the code is failing after a call to `makeBlock`, then that function (not shown) comes under suspicion. In general, this kind of problem is much quicker to solve by stepping through your program with a debugger. – Passerby Jan 04 '22 at 05:51
  • I edited to include makeBlock(). It's confusing to me because I am allocating in makeBlock, and it's not shown here but I was using printf("%p") to see if the pointer location existed and it does. – theretrogamer Jan 04 '22 at 05:58
  • makeBlock does not initialise block->nextBlock. – n. m. could be an AI Jan 04 '22 at 06:07
  • 1
    I suspect that `-fsanitize=address` will find the problem instantly. – ikegami Jan 04 '22 at 06:31
  • I fixed it by initializing block->nextBlock, but what is -fsanitize? My first time ever hearing about that. – theretrogamer Jan 04 '22 at 06:46
  • 1
    With `if(chunk->nodeBlock->nextBlock == NULL)` you are checking a nested pointer. Did you check non-NULL for `chunk` and for `chunk->nodeBlock` ? – Yunnosch Jan 04 '22 at 06:59
  • @theretrogamer see https://stackoverflow.com/q/37970758/17635987 – kirjosieppo Jan 04 '22 at 09:54

0 Answers0