Uwp 使用Win2D CanvasDrawingSession的内存泄漏
我创建了一个图像效果,它将时间覆盖在现有图像上。我遇到的问题似乎是每次在Win2D CanvasDrawingSession上使用DrawImage或DrawText时都会出现持久内存泄漏。随着时间的推移,这变得非常重要。我肯定在释放资源方面遗漏了什么,但我看不到。如果我注释掉代码的DrawImage和DrawText行,内存泄漏就会停止Uwp 使用Win2D CanvasDrawingSession的内存泄漏,uwp,c++-cli,win2d,Uwp,C++ Cli,Win2d,我创建了一个图像效果,它将时间覆盖在现有图像上。我遇到的问题似乎是每次在Win2D CanvasDrawingSession上使用DrawImage或DrawText时都会出现持久内存泄漏。随着时间的推移,这变得非常重要。我肯定在释放资源方面遗漏了什么,但我看不到。如果我注释掉代码的DrawImage和DrawText行,内存泄漏就会停止 virtual void ProcessFrame(ProcessVideoFrameContext^ context) {
virtual void ProcessFrame(ProcessVideoFrameContext^ context)
{
time_t curtime = time(0);
char buffer[16];
struct tm info;
localtime_s(&info, &curtime);
strftime(buffer, sizeof(buffer), "%H:%M:%S", &info);
std::string stime = std::string(buffer);
std::wstring w_str = std::wstring(stime.begin(), stime.end());
String^ sTime = ref new String(w_str.c_str());
CanvasDevice^ device = ref new CanvasDevice();
auto newBitmap = SoftwareBitmap::Convert(context->InputFrame->SoftwareBitmap, BitmapPixelFormat::Bgra8);
auto inputBitmap = CanvasBitmap::CreateFromSoftwareBitmap(device, newBitmap);
auto frameSize = inputBitmap->SizeInPixels.Width * inputBitmap->SizeInPixels.Height * 4;
auto frameBuffer = ref new Windows::Storage::Streams::Buffer((unsigned int)frameSize);
auto renderTarget = ref new CanvasRenderTarget(device, (float)inputBitmap->SizeInPixels.Width, (float)inputBitmap->SizeInPixels.Height, (float)inputBitmap->Dpi);
auto ds = renderTarget->CreateDrawingSession();
// ds->DrawImage(inputBitmap);
auto format = ref new CanvasTextFormat();
format->FontFamily = _fontFamily;
format->FontSize = (float)_fontSize;
// ds->DrawText(sTime, (float)_offsetX, (float)_offsetY, _fontColor, format);
delete ds;
renderTarget->GetPixelBytes(frameBuffer);
SoftwareBitmap^ nxtBitmap = ref new SoftwareBitmap(BitmapPixelFormat::Bgra8, newBitmap->PixelWidth, newBitmap->PixelHeight);
nxtBitmap->CopyFromBuffer(frameBuffer);
SoftwareBitmap^ anBitmap = SoftwareBitmap::Convert(nxtBitmap, BitmapPixelFormat::Yuy2);
anBitmap->CopyToBuffer(frameBuffer);
// Copy the modified frame data to the output frame.
context->OutputFrame->SoftwareBitmap->CopyFromBuffer(frameBuffer);
delete format;
delete nxtBitmap;
delete anBitmap;
delete inputBitmap;
delete frameBuffer;
delete renderTarget;
delete sTime;
delete device;
delete newBitmap;
}
我发现这是因为在每一帧上创建了一个新的画布设备。一旦我把它移到一个全局设备上,泄漏情况就自行解决了。我发现这是因为在每一帧上创建了一个新的画布设备。一旦我把它移到一个全球设备上,泄漏情况就自行解决了