gpt4 book ai didi

c++ - munmap_chunk() - 无效指针错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:17 25 4
gpt4 key购买 nike

我正在使用低级 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)

最佳答案

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

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

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

关于c++ - munmap_chunk() - 无效指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com