简体   繁体   中英

std::vector using back(), pop_back(), push_back(), get 'double free or corruption' error

I have the following code:

struct branchInfo{
    Quaternion r;
    Vector3 p;

    std::vector<Vector3> branchPoints;
};

std::vector<Vector3> LSystem::Turtle(){

    std::vector<branchInfo> b;

    // The axiom should always start with a '[' at the start and a ']' at the end
    for(unsigned int i = 0; i < axiom.length(); i++) {
        char c = axiom[i];

        if (c == '[') {
            branchInfo temp;

            temp.r = rotationQuat;
            temp.p = position;
            temp.branchPoints.push_back(position);

            b.push_back(temp);
        } else if (c == ']') {
            branchInfo temp = b.back();

            rotationQuat = temp.r;
            position = temp.p;
            branches.push_back(temp.branchPoints);

            b.pop_back();
        } else {
            // Evaluate the character and move the turtle
            // F = move forward according to pitched, rolled, and yawed axis
            // f = move backward according to pitched, rolled, and yawed axis
            // p = pitch -45 degrees
            // P = pitch +45 degrees
            // r = roll -45 degrees
            // R = Roll +45 degrees
            // y = yaw -45 degrees
            // Y = yaw +45 degrees
            // [ = start new branch
            // ] = end new branch

            switch(c) {
            case 'f':
                b.back().branchPoints.push_back(Forward(-1.5f));
                break;
            case 'F':
                b.back().branchPoints.push_back(Forward(1.5f));
                break;
            case 'p':
                Pitch(-angle);
                break;
            case 'P':
                Pitch(angle);
                break;
            case 'r':
                Roll(-angle);
                break;
            case 'R':
                Roll(angle);
                break;
            case 'y':
                Yaw(-angle);
                break;
            case 'Y':
                Yaw(angle);
                break;
            }
        }
    }
}

The point of this code is to evaluate the axiom string which the turtle will follow. This is an L-system and part of the point of an L-system is to have branching and that occurs when there is a '[' character which means the rotation and position get saved and will be used later. As an example, If my L-system class contains the string "[PPPPrrFp[[X]PX]pXRR]" I will get the following error: *** Error in /home/user/program/dist/Debug/GNU-Linux/world': double free or corruption (fasttop): 0x0000000003f43240 ***

I know it is not easy to step-by-step go through this program and I don't expect anyone to do that. I'm just wondering if it is easy to see what is wrong with the program. I understand that the error probably has to do with using some of the std::vector methods/functions but I'm am not seeing why or how it is causing it.

Also, it is not consistent. Sometimes the program will run and without a hitch, and other times it will crash with the error I mentioned.

Functions for Yaw, Pitch, and Roll should not matter.

From what I have googled, I can only ever find examples of this error when dealing with classes that have constructors, destructors, and when deleting arrays, etc.

Thanks for any help! I hope it is obvious what is happening and doesn't take too much time. If it does look like it will take too long then do not bother. Thanks again.

EDIT: To make it eaiser to understand what is happening in the function, I will do a step-by-step explanation of the function: Start at index 0 of the axiom string and check if it starts a new branch ('['), and if it does then push/save 3D vector pos and rot -> if the current char is not an '[' or ']' then move pos or rotate -> if the branch ends (']') resusme from the last saved pos and rot, and pop the top of the stack -> keep going for every char in the string.

In case someone from the future comes across this problem. What I was doing wrong was not returning anything in my function even though I specified a non-void return type for the function. Changing the return type to void solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM