munmap_chunk() - 指针错误无效

时间:2014-04-02 16:15:24

标签: c++ memory memory-management sdl glm-math

我正在使用低级SDL函数编写渲染器,以了解它是如何工作的。我现在正在尝试绘制多边形,但由于我缺乏C ++经验,我遇到了错误。运行代码时出现munmap_chunk() - Invalid pointer错误。搜索显示它很可能是因为两次释放内存。从函数返回时发生错误。我意识到错误来自自动free()内存之前已自动free() d,但我对C ++没有足够的经验来发现错误。有线索吗?

我的代码:

void DrawPolygon (const vector<vec3> & verts, vec3 color){

    // 0. Project to the screen
    vector<ivec2> vertices(verts.size());
    for(int i = 0; i < verts.size(); i++){
        VertexShader(verts.at(i), vertices.at(i));
    }

    // 1. Find max and min y-value of the polygon 
    // and compute the number of rows it occupies.
    int miny = vertices[0].y;
    int maxy = vertices[0].y;

    for (int i = 1; i < 3; i++){
        if (vertices[i].y < miny){
            miny = vertices[i].y;
        }
        if (vertices[i].y > maxy){
            maxy = vertices[i].y;
        }
    }

    int rows = abs(maxy - miny) + 1;
    // 2. Resize leftPixels and rightPixels 
    // so that they have an element for each row. 

    vector<ivec2> leftPixels(rows);
    vector<ivec2> rightPixels(rows);

    // 3. Initialize the x-coordinates in leftPixels 
    // to some really large value and the x-coordinates 
    // in rightPixels to some really small value. 

    for(int i = 0; i < rows; i++){
        leftPixels[i].x = std::numeric_limits<int>::max();
        rightPixels[i].x = std::numeric_limits<int>::min();
        leftPixels[i].y = miny + i;
        rightPixels[i].y = miny + i;
    }

    // 4. Loop through all edges of the polygon and use 
    // linear interpolation to find the x-coordinate for 
    // each row it occupies. Update the corresponding 
    // values in rightPixels and leftPixels.

    for(int i = 0; i < 3; i++){
        ivec2 a = vertices[i];
        ivec2 b = vertices[(i+1)%3];

        // find the number of pixels to draw
        ivec2 delta = glm::abs(a - b);
        int pixels = glm::max(delta.x, delta.y) + 1;

        // interpolate to find the pixels
        vector<ivec2> line (pixels);
        Interpolate(a, b, line);

        for(int j = 0; j < pixels; j++){
            ivec2 p = line[j];
            ivec2 cmpl = leftPixels[p.y - miny];
            ivec2 cmpr = rightPixels[p.y - miny];

            if(p.x < cmpl.x){
                leftPixels[p.y - miny].x = p.x;
                //leftPixels[p.y - miny] = cmpl;
            }

            if(p.x > cmpr.x){
                rightPixels[p.y - miny].x = p.x;
                //cmpr.x = p.x;
                //rightPixels[p.y - miny] = cmpr;
            }
        }
    }

    for(int i = 0; i < leftPixels.size(); i++){
        ivec2 l = leftPixels.at(i);
        ivec2 r = rightPixels.at(i);

        // y coord the same, iterate over x
        int y = l.y;
        for(int x = l.x; x <= r.x; x++){
            PutPixelSDL(screen, x, y, color);
        }
    }
}

使用valgrind给我这个输出(这是它报告的第一个错误)。奇怪的是,该程序恢复并继续运行预期的结果,显然没有再次得到相同的错误:

==5706== Invalid write of size 4
==5706==    at 0x40AD61: DrawPolygon(std::vector<glm::detail::tvec3<float>, std::allocator<glm::detail::tvec3<float> > > const&, glm::detail::tvec3<float>) (in /home/actimia/prog/dgi14/lab3/ThirdLab)
==5706==    by 0x409C78: Draw() (in /home/actimia/prog/dgi14/lab3/ThirdLab)
==5706==    by 0x409668: main (in /home/actimia/prog/dgi14/lab3/ThirdLab)

1 个答案:

答案 0 :(得分:1)

我认为我之前关于类似主题的帖子会很有用。

https://stackoverflow.com/a/22658693/2724703

从Valgrind报告中,看起来您的程序因溢出而导致内存损坏。这似乎不是&#34;双重免费&#34;错误(这是溢出方案)。您已经提到有时valgrind没有报告任何错误,这会使这个问题变得更加困难。然而,肯定存在内存损坏,您必须修复它们。由于各种原因(不同的输入参数,多线程,执行顺序的更改),内存错误有时会间歇性地发生。