Heap Interlace Error

I get this eror from Visual C ++:

HEAP [ShockRay3.exe]: heap block in 00557018, changed to 00557044, last requested size 24

This happens when I add a line of code between two recursive calls in a function. The entire line of code makes this pointer change, but for some reason, every time I insert it, I get this error.

This is a simplified version of the code (only memory heap memory calls a function)

int KDTree::BuildBranch(int height, Mailbox** objs, int nObjects)
{
    {...}
        //Check for termination
    if(height == -1 || nObjects < minObjectsPerNode)
    {
        {...}

        return nodeIndex - 1;
    }

    //Save this node index and increment the current index to save space for this node
    BoundingBox* tempBox = new BoundingBox();
    //If this is first voxel, we don't keep track of stradle counts
    if(nodeIndex == 1)
    {
        {...}

        for(int i = 0; i < nObjects; i++)
        {
            //Get bounding box
            objs[i]->prim->MakeBoundingBox(tempBox);

            //Add mins to split lists
            xMins[index] = tempBox->x0;
            yMins[index] = tempBox->y0;
            zMins[index] = tempBox->z0;

            //Add maxs
            xMaxs[index] = tempBox->x1;
            yMaxs[index] = tempBox->y1;
            zMaxs[index] = tempBox->z1;
            index++;
        }
    }
    else
    {
        for(int i = 0; i < nObjects; i++)
        {
            //Get bounding box
            objs[i]->prim->MakeBoundingBox(tempBox);

            //Add mins to split lists checking for straddle
            if(tempBox->x0 < curVoxelBounds->x0)
            {
                {...}
            }
            else
            {
                xMins[xMinCount] = tempBox->x0;
                {...}
            }

            if(tempBox->y0 < curVoxelBounds->y0)
            {
                {...}
            }
            else
            {
                yMins[yMinCount] = tempBox->y0;
                {...}
            }

            if(tempBox->z0 < curVoxelBounds->z0)
            {
                {...}
            }
            else
            {
                zMins[zMinCount] = tempBox->z0;
                {...}
            }

            //Add maxs to split lists checking for straddle
            if(tempBox->x1 > curVoxelBounds->x1)
            {
                {...}
            }
            else
            {
                xMaxs[xMaxCount] = tempBox->x1;
                {...}
            }

            if(tempBox->y1 > curVoxelBounds->y1)
            {
                {...}
            }
            else
            {
                yMaxs[yMaxCount] = tempBox->y1;
                {...}
            }

            if(tempBox->z1 > curVoxelBounds->z1)
            {
                {...}
            }
            else
            {           
                zMaxs[zMaxCount] = tempBox->z1;
                {...}   
            }
        }
    }

    //If this is the root node, construct the scene bounding box
    if(nodeIndex == 1)
    {
        bb = new BoundingBox(xMins[0], xMaxs[nObjects - 1], yMins[0], yMaxs[nObjects - 1], zMins[0], zMaxs[nObjects - 1]);
        curVoxelBounds = new BoundingBox(xMins[0], xMaxs[nObjects - 1], yMins[0], yMaxs[nObjects - 1], zMins[0], zMaxs[nObjects - 1]);
    }

    {...}

    //Allocate space for left and right lists
    Mailbox** leftList = new Mailbox*[minLeftCounter];
    Mailbox** rightList = new Mailbox*[minRightCounter];

    //Sort objects into lists of those to the left and right of the split plane
    //Bounding box for left and right
    BoundingBox* rightBox = NULL;
    BoundingBox* leftBox = NULL;
    //Saved pointer to current bounding box
    BoundingBox* savedBox = curVoxelBounds;

    int leftIndex = 0, rightIndex = 0;
    {...}
    switch(axis)
    {
    case 0:
        for(int i = 0; i < nObjects; i++)
        {
            //Get object bounding box
            objs[i]->prim->MakeBoundingBox(tempBox);

            //Add to left and right lists when necessary
            if(tempBox->x0 < splitLoc)
            {
                {...}
            }

            if(tempBox->x1 > splitLoc)
            {
                {...}
            }
        }

        //Construct new bounding boxes
        leftBox = new BoundingBox(curVoxelBounds->x0, splitLoc, curVoxelBounds->y0,
            curVoxelBounds->y1, curVoxelBounds->z0, curVoxelBounds->z1);
        rightBox = new BoundingBox(splitLoc, curVoxelBounds->x1, curVoxelBounds->y0,
            curVoxelBounds->y1, curVoxelBounds->z0, curVoxelBounds->z1);
        break;

    case 1:
        for(int i = 0; i < nObjects; i++)
        {
            //Get object bounding box
            objs[i]->prim->MakeBoundingBox(tempBox);

            //Add to left and right lists when necessary
            if(tempBox->y0 < splitLoc)
            {
                {...}
            }

            if(tempBox->y1 > splitLoc)
            {
                {...}
            }

        }

        //Construct new bounding boxes
        leftBox = new BoundingBox(curVoxelBounds->x0, curVoxelBounds->x1, curVoxelBounds->y0,
            splitLoc, curVoxelBounds->z0, curVoxelBounds->z1);
        rightBox = new BoundingBox(curVoxelBounds->x0, curVoxelBounds->x1, splitLoc,
            curVoxelBounds->y1, curVoxelBounds->z0, curVoxelBounds->z1);
        break;

    case 2:
        for(int i = 0; i < nObjects; i++)
        {
            //Get object bounding box
            objs[i]->prim->MakeBoundingBox(tempBox);

            //Add to left and right lists when necessary
            if(tempBox->z0 < splitLoc)
            {
                {...}
            }

            if(tempBox->z1 > splitLoc)
            {
                {...}
            }

        }

        //Construct new bounding boxes
        leftBox = new BoundingBox(curVoxelBounds->x0, curVoxelBounds->x1, curVoxelBounds->y0,
            curVoxelBounds->y1, curVoxelBounds->z0, splitLoc);
        rightBox = new BoundingBox(curVoxelBounds->x0, curVoxelBounds->x1, curVoxelBounds->y0,
            curVoxelBounds->y1, splitLoc, curVoxelBounds->z1);
        break;
    };

    //Delete the bounding box
    delete tempBox;

    //Delete old objects array
    delete[] objs;

    {...}

    //Change bounding box
    curVoxelBounds = leftBox;
    //Build the left branch
    BuildBranch(height - 1, leftList, leftCount);

    //Change bounding box
    curVoxelBounds = rightBox; //<----THIS IS THE LINE RESULTING IN THE ERROR
    //Build the right branch
    int rcNodeIndex = BuildBranch(height - 1, rightList, rightCount);

    //Restore bounding box
    curVoxelBounds = savedBox;

    //Delete left and right bounding boxes
    delete leftBox;
    delete rightBox;

    {...}

    return thisNodeIndex;
}

If I take this line where I change the pointer, the program works. If I leave it, it does not work, and the call stack shows this line:

delete[] objs;

but before that, in the call stack, this is the line that apparently calls it. I do not know how changing a pointer can transcode the code to this delete line.

+3
3

, , . , :

  • leftCount rightCount, ?
  • leftList rightList ?
  • , BuildBranch , ?
  • , ?

, :

  • , ? ?
  • , , - ? , , ?
+2

( !). , , , . , , , .

, , :

  • - KDTree, - .
  • NULL - , .
  • - Assert not-null .
  • , curVoxelBounds
+2

You can try Application Verifier . This is pretty good when looking for memory overwrites and related problems.

Here is one of the important details about Application Verifier. You can set full page authentication . If your application has heap overflow or overflow, it will be caught immediately. And it is extremely easy to use. And for free.

0
source

Source: https://habr.com/ru/post/1726953/


All Articles