Poul K. Sørensen
Poul K. Sørensen

Reputation: 17530

0xc0000374 Heap Corruption in Cli/Cpp code when debugging is not attched

I am running into a problem with a Heap Corruption in my Cli/cpp project.

Things only seem to give issues when a debugger is not attached or not profiling using dependency walker tools.

It seems to me that the application is first crashing at the end when freeing up some memory.

If anyone could take a look at tell me if I am doing something really bad in the following code that could cause this.

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();

Upvotes: 1

Views: 1077

Answers (1)

Poul K. S&#248;rensen
Poul K. S&#248;rensen

Reputation: 17530

So the error was even before the code I posted above.

by taking one line at a time and returning I was able to find the cause of my heap corruption.

double pxpy[2] = { (double)xmin * tileSize, (double)ymin * tileSize };  
pixelsToMeters(pxpy, zoom, initialResolution, mxmy);

above works and below was causing the error

pixelsToMeters(new double[] { (double)xmin * tileSize, (double)ymin * tileSize }, zoom, initialResolution, mxmy);

Upvotes: 1

Related Questions