我有一个简单的洪水填充功能,除了它在运行时因过多的释放而崩溃。
#include <vector>
cv::Mat fillLayer(cv::Mat filledEdge, int y, int x, int oldColor, float newColor){
cv::Size shape = filledEdge.size();
int h = shape.height;
int w = shape.width;
std::vector<int> theStackx = {x};
std::vector<int> theStacky = {y};
while (theStacky.size() > 0){
y = theStacky.back();
x = theStackx.back();
theStacky.pop_back();
theStackx.pop_back();
if (x == w){
continue;
}
if (x == -1){
continue;
}
if (y == -1){
continue;
}
if (y == h){
continue;
}
if (filledEdge.at<float>(y, x) != oldColor){
continue;
}
filledEdge.at<float>(y, x) = newColor;
//up
theStacky.push_back(y + 1);
theStackx.push_back(x);
//down
theStacky.push_back(y - 1);
theStackx.push_back(x);
//right
theStacky.push_back(y);
theStackx.push_back(x + 1);
//left
theStacky.push_back(y);
theStackx.push_back(x - 1);
}
return filledEdge;
}
贯穿floodfill的函数是fillSurface。它贯穿 Mat 中的所有像素,并为每个 Floodfill 填充不同的颜色
fillSurface(cv::Mat filledEdge, int oldColor) {
std::vector<float> layers; //list all the different colors in mat
cv::Size shape = filledEdge.size();
int h = shape.height;
int w = shape.width;
float newColor;
// run through all the pixels in Mat
for(int y = 0; y!= h; y++){
for(int x = 0; x!= w; x++){
// only run floodfill if current pixel is oldColor
if (filledEdge.at<float>(y, x) == oldColor){
//newColor is random float to fill in to floodfill
newColor = static_cast <float> ((rand()) / (static_cast <float> (RAND_MAX/253)) + 1);
// add newColor to list of layers
layers.push_back(newColor);
//run flood fill replacing old color with new color
filledEdge = fillLayer(filledEdge, y, x, oldColor, newColor);
}
}
}
}
这是我收到的错误:
Incorrect checksum for freed object 0x7fea0d89dc00: probably modified after being freed.
我所做的调试远是在 malloc_error_break() 上设置一个断点,以查看我在哪里得到了中断。它确实导致了洪水填充功能。
我想知道是否有办法解决这个问题。如果没有,最好的选择是什么?
