UsingNet / nvjpeg-python

nvjpeg for python
MIT License
93 stars 24 forks source link

内存泄漏 #23

Open ZhangYich opened 6 months ago

ZhangYich commented 6 months ago

不知道为什么,把下面函数返回的 unsigned char* 值给cv::Mat之后会导致内存泄漏

unsigned char* JpegCoderImage::buffer() {
    nvjpegImage_t* img = ((nvjpegImage_t*)(this->img));
    size_t size = height * width * 3;
    unsigned char* buffer = (unsigned char*)malloc(size);
    cudaMemcpy(buffer, img->channel[0], size, cudaMemcpyDeviceToHost);

    return buffer;
}
JpegCoderImage* jpgImage = decode((const unsigned char *)(pchData.data()), file_size);

img = cv::Mat(jpgImage->height, jpgImage->width, CV_8UC3, jpgImage->buffer());

delete jpgImage;

好像是 buffer 导致内存无法释放?

我现在的做法是:

unsigned char* JpegCoderImage::buffer() {
    nvjpegImage_t* img = ((nvjpegImage_t*)(this->img));
    //size_t size = height * width * 3;
    //unsigned char* buffer = (unsigned char*)malloc(size);
    //cudaMemcpy(buffer, img->channel[0], size, cudaMemcpyDeviceToHost);

    return img->channel[0];
}
JpegCoderImage* jpgImage = decode((const unsigned char *)(pchData.data()), file_size);

img = cv::Mat(jpgImage->height, jpgImage->width, CV_8UC3);

cudaMemcpy(img.data, jpgImage->buffer(), jpgImage->height * jpgImage->width * 3, cudaMemcpyDeviceToHost);

delete jpgImage;