Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

内存泄漏 #23

Open
ZhangYich opened this issue May 11, 2024 · 0 comments
Open

内存泄漏 #23

ZhangYich opened this issue May 11, 2024 · 0 comments

Comments

@ZhangYich
Copy link

ZhangYich commented May 11, 2024

不知道为什么,把下面函数返回的 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant