我在Cli / cpp项目中遇到了堆腐败问题。
当没有附加调试器或没有使用依赖性walker工具进行分析时,事情似乎只会出现问题。
在我看来,应用程序在释放一些内存时最终会崩溃。
如果有人可以看看我是否在下面的代码中做了一些非常糟糕的事情,可能会导致这种情况。
std::string imagePath = msclr::interop::marshal_as< std::string >(Path::Combine(this->context->WorkingDirectory, this->context->Payload->ImagePath));
std::cout << imagePath << std::endl;
cv::Mat image = cv::imread(imagePath);
if (!image.data)
{
throw gcnew System::Exception("image data is not loaded");
}
std::vector<cv::Point2f> src;
src.push_back(cv::Point2f(0, 0));
src.push_back(cv::Point2f(0, image.rows));
src.push_back(cv::Point2f(image.cols, image.rows));
src.push_back(cv::Point2f(image.cols, 0));
cv::Mat M = cv::getPerspectiveTransform(src, dst);
cv::Mat warp_img((ymax - ymin + 1)*tileSize, (xmax - xmin + 1)*tileSize, CV_8UC3);
std::vector<cv::Point2f> obj_corners(4);
obj_corners[0] = cv::Point(0, 0); obj_corners[1] = cv::Point(image.cols, 0);
obj_corners[2] = cv::Point(image.cols, image.rows); obj_corners[3] = cv::Point(0, image.rows);
std::vector<cv::Point2f> scene_corners(4);
cv::perspectiveTransform(obj_corners, scene_corners, M);
cv::warpPerspective(image, warp_img, M, cv::Size((xmax - xmin + 1)*tileSize, (ymax - ymin + 1)*tileSize));
image.release();
cv::Mat warp_img_fliped;
cv::flip(warp_img, warp_img_fliped, 0);
warp_img.release();
String^ id = Guid::NewGuid().ToString();
std::vector<int> compression_params;
if (this->context->Payload->Extension->Equals(".png")){
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
}
if (this->context->Payload->Extension->Equals(".jpg")){
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(this->context->Payload->Quality);
}
for (int r = 0, rr = (ymax - ymin + 1)*tileSize, ty_idx = ymax; r < rr; r += tileSize, ty_idx--){
for (int c = 0, cc = (xmax - xmin + 1)*tileSize, tx_idx = xmin; c < cc; c += tileSize, tx_idx++)
{
std::cout << ty_idx << " " << tx_idx << std::endl;
cv::Mat tile = warp_img_fliped(cv::Rect(c, r, tileSize, tileSize));
String^ Path = Path::Combine(this->context->WorkingDirectory, id,String::Format("{0}\\{1}\\{2}\\{3}", zoom,tx_idx,ty_idx, this->context->Payload->Extension));
Directory::CreateDirectory(Path::GetDirectoryName(Path));
std::string path = msclr::interop::marshal_as< std::string >(Path);
cv::imwrite(path, tile, compression_params);
}
}
warp_img_fliped.release();
答案 0 :(得分:1)
所以错误甚至在我上面发布的代码之前。
一次取一行并返回我能找到堆损坏的原因。
double pxpy[2] = { (double)xmin * tileSize, (double)ymin * tileSize };
pixelsToMeters(pxpy, zoom, initialResolution, mxmy);
以上作品及以下内容导致错误
pixelsToMeters(new double[] { (double)xmin * tileSize, (double)ymin * tileSize }, zoom, initialResolution, mxmy);