Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

vc::resize will change the dst Mat::data buf address

#include <stdio.h>
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
usng namespace std;
using namespace cv;


int main(int argc, char **argv)
{
    char* data = (char*)malloc(100*100); 
    Mat img = imread("./a.jpg");
    Mat newImg = cv::Mat(100, 100,  CV_8SC3, (void*)data); //transfer the data buf to Mat instance newImg
    printf("data addr:%#x newImage.data:%#x\n", data, newImg.data);  //newImg.data addr equeals to data addr

    cv::resize(img, newImg, newImg.size(), 0, 0, cv::INTER_LINEAR);
    printf("newImg.data ater resize:%#x\n", newImg.data);//newImage.data changed by cv::resize

    free(data);
    img.release(); 
    return 1;
}

Result running this code:

data addr:0xa49a8360 newImage.data:0xa49a8360
newImg.data ater resize:0xa49b1cc0

Perhaps opencl API clSetKernelArg will be called and then moved the data buf.

And there will be a memory leak of this code detected by valgrind.

vc::resize will change the dst Mat::data buf address

include <stdio.h>

#include <stdio.h>
#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
usng namespace std;
using namespace cv;


int main(int argc, char **argv)
{
    char* data = (char*)malloc(100*100); 
    Mat img = imread("./a.jpg");
    Mat newImg = cv::Mat(100, 100,  CV_8SC3, (void*)data); //transfer the data buf to Mat instance newImg
    printf("data addr:%#x newImage.data:%#x\n", data, newImg.data);  //newImg.data addr equeals to data addr

    cv::resize(img, newImg, newImg.size(), 0, 0, cv::INTER_LINEAR);
    printf("newImg.data ater resize:%#x\n", newImg.data);//newImage.data changed by cv::resize

    free(data);
    img.release(); 
    return 1;
}

Result running this code:

data addr:0xa49a8360 newImage.data:0xa49a8360
newImg.data ater resize:0xa49b1cc0

Perhaps opencl API clSetKernelArg will be called and then moved the data buf.buf, and will cause an extra mem allocate and memcpy consumption.

And there will be a memory leak of this code detected by valgrind.

vc::resize will change the dst Mat::data buf address

include <stdio.h>

#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
usng namespace std;
using namespace cv;


int main(int argc, char **argv)
{
    char* data = (char*)malloc(100*100); 
    Mat img = imread("./a.jpg");
    Mat newImg = cv::Mat(100, 100,  CV_8SC3, (void*)data); //transfer the data buf to Mat instance newImg
    printf("data addr:%#x newImage.data:%#x\n", data, newImg.data);  //newImg.data addr equeals to data addr

    cv::resize(img, newImg, newImg.size(), 0, 0, cv::INTER_LINEAR);
    printf("newImg.data ater resize:%#x\n", newImg.data);//newImage.data changed by cv::resize

    free(data);
    img.release(); 
    return 1;
}

Result running this code:

data addr:0xa49a8360 newImage.data:0xa49a8360
newImg.data ater resize:0xa49b1cc0

Perhaps opencl API clSetKernelArg will be called and then moved the data buf, and will cause an extra mem allocate and memcpy consumption.

And there will be a memory leak of this code detected by valgrind.

vc::resize will change the dst Mat::data buf addressaddress unexpectedly.

include <stdio.h>

#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
usng namespace std;
using namespace cv;


int main(int argc, char **argv)
{
    char* data = (char*)malloc(100*100); 
    Mat img = imread("./a.jpg");
    Mat newImg = cv::Mat(100, 100,  CV_8SC3, (void*)data); //transfer the data buf to Mat instance newImg
    printf("data addr:%#x newImage.data:%#x\n", data, newImg.data);  //newImg.data addr equeals to data addr

    cv::resize(img, newImg, newImg.size(), 0, 0, cv::INTER_LINEAR);
    printf("newImg.data ater resize:%#x\n", newImg.data);//newImage.data changed by cv::resize

    free(data);
    img.release(); 
    return 1;
}

Result running this code:

data addr:0xa49a8360 newImage.data:0xa49a8360
newImg.data ater resize:0xa49b1cc0

Perhaps opencl API clSetKernelArg will be called and then moved the data buf, and will cause an extra mem allocate and memcpy consumption.

And there will be a memory leak of this code detected by valgrind.

vc::resize will change the dst Mat::data buf address unexpectedly.

include <stdio.h>

#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
usng namespace std;
using namespace cv;


int main(int argc, char **argv)
{
    char* data = (char*)malloc(100*100); 
    Mat img = imread("./a.jpg");
    Mat newImg = cv::Mat(100, 100,  CV_8SC3, (void*)data); //transfer the data buf to Mat instance newImg
    printf("data addr:%#x newImage.data:%#x\n", data, newImg.data);  //newImg.data addr equeals to data addr

    cv::resize(img, newImg, newImg.size(), 0, 0, cv::INTER_LINEAR);
    printf("newImg.data ater resize:%#x\n", newImg.data);//newImage.data changed by cv::resize

    free(data);
    img.release(); 
    return 1;
}

Result running this code:

data addr:0xa49a8360 newImage.data:0xa49a8360
newImg.data ater resize:0xa49b1cc0

Perhaps opencl API clSetKernelArg will be called and then moved the data buf, and will cause an extra mem allocate and memcpy consumption.

And there will be a memory leak of this code detected by valgrind.

click to hide/show revision 6
None

updated 2018-12-06 01:16:32 -0600

berak gravatar image

vc::resize will change the dst Mat::data buf address unexpectedly.

include <stdio.h>

#include <string>
#include <iostream>
#include <opencv2/opencv.hpp>
usng namespace std;
using namespace cv;


int main(int argc, char **argv)
{
    char* data = (char*)malloc(100*100); 
    Mat img = imread("./a.jpg");
    Mat newImg = cv::Mat(100, 100,  CV_8SC3, (void*)data); //transfer the data buf to Mat instance newImg
    printf("data addr:%#x newImage.data:%#x\n", data, newImg.data);  //newImg.data addr equeals to data addr

    cv::resize(img, newImg, newImg.size(), 0, 0, cv::INTER_LINEAR);
    printf("newImg.data ater resize:%#x\n", newImg.data);//newImage.data changed by cv::resize

    free(data);
    img.release(); 
    return 1;
}

Result running this code:

data addr:0xa49a8360 newImage.data:0xa49a8360
newImg.data ater resize:0xa49b1cc0

Perhaps opencl API clSetKernelArg will be called and then moved the data buf, and will cause an extra mem allocate and memcpy consumption.

And there will be a memory leak of this code detected by valgrind.