0

I am doing Direct2D show some text (like fps, resolution etc) on Direct3D surface. The weird thing is that in my Window Class there is a method called CalculateFrameStats() where every loop, use this to calcualte the FPS etc information and use Direct2D IDWriteFactory::CreateTextLayout to create a new Textlayout with latest updated FPS text strings. And do BeginDraw(), DrawTextLayout(), EndDraw() in the 3DFrameDraw() function. And after that I don't release the TextLayout pointer. And next round goes to CalculateFrameStats(), it CreateTextLayout again with newly update FPS etc strings. And in 3DFrameDraw() function, I drawTextlayout again. And it loops like this over and over. But when I run the program, it seems no memory leaks at all, the memory usage keeps low and constant.

But when put IDWriteFactory::CreateTextLayout in 3DFrameDraw() function, which means every 3D frame draw in the beginning I create a new TextLayout with updated FPS string and do some 3D manipulations and before D3D-present, I do BeginDraw(), DrawTextLayout(), EndDraw(). This is the same area in previous 3DFrameDraw(). But this time, the memory leaks, and I can see the memory keep growing when time elapse. But if I add Textlayout pointer->release() after BeginDraw(), DrawTextLayout(), EndDraw(), the memory leaks gone.

I don't really know why the first scenario Textlayout pointer never got release until close the program, the memory never leaks. Does TextLayout need to be released every time/frame when update its text string?

Bill Banks
  • 55
  • 1
  • 8
  • Every CreateTextLayout must be paired with a textLayout->Release, that's all there is. The usage of smart pointers (like ATL's CComPtr or WRL's ComPtr, etc.) is recommended to ensure this. In your case, since a text layout is a device-independent resource, you can create it just once for the whole program run. Otherwise show us some real code instead of words. – Simon Mourier Oct 17 '21 at 06:09
  • It doesn't really matter if it's device dependent or not. If you want to draw different text, you'll have to recreate layout object. – bunglehead Dec 24 '21 at 11:43

0 Answers0