【问题标题】:munmap_chunk() - Invalid pointer errormunmap_chunk() - 无效指针错误
【发布时间】:2014-05-14 01:56:17
【问题描述】:

我正在编写一个使用低级 SDL 函数的渲染器,以了解它是如何工作的。我现在正在尝试绘制多边形,但可能由于我对 C++ 的缺乏经验而遇到错误。运行代码时出现munmap_chunk() - Invalid pointer 错误。搜索显示,这很可能是由于两次 free()-ing 内存。从函数返回时发生错误。我意识到错误来自自动free()ing 内存,之前自动为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)

【问题讨论】:

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


    【解决方案1】:

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

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

    根据您的 Valgrind 报告,您的程序似乎由于溢出而导致内存损坏。这看起来不像“双重释放”错误(这是溢出场景)。您提到有时 valgrind 没有报告任何错误,这使这个问题变得更加困难。但是,肯定存在内存损坏,您必须修复它们。由于各种原因(不同的输入参数,多线程,执行顺序的变化),有时会间歇性发生内存错误。

    【讨论】:

    • 谢谢。由于“向后”时的插值舍入,这是一个超出范围的错误。
    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 2020-08-04
    • 2018-08-23
    • 1970-01-01
    • 2020-09-16
    • 2011-09-06
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多