canvas icon indicating copy to clipboard operation
canvas copied to clipboard

Memory leak of `text` & app crash on `encode('png')`

Open MarosPistej opened this issue 2 years ago • 5 comments

Discovered memory leak while drawing text and segmentation error app crash when encoding

I created minimum reproducible example repository here https://github.com/MarosPistej/napi-rs-canvas-text-mem-leak

MarosPistej avatar Jan 27 '22 15:01 MarosPistej

I have also encountered this leak, I think the encoding crash might be a separate issue (related to how skia handles the underlying buffer, for example if you pass the buffer to another tool like sharp without copying it first, then you'll get the seg error). Also the leak can occur even when the encoding step doesn't take place.

louisfoster avatar Feb 10 '22 13:02 louisfoster

I'm wondering if paragraph needs to have reset() called at the end? I'm not a pro at C++, so I'm just trying to understand the cause of the leak, but looking at how paragraph is created it appears to use make_unique, which from my understanding is a kind of smarter/safer alternative to new. Although, reading this SO question, it is pointed out that something created via make_unique should subsequently called reset() to free the memory. However, I noticed in the code in my first link, there's no call to reset.

louisfoster avatar Feb 10 '22 14:02 louisfoster

I may have been slightly incorrect, but this reference says that if release() is called, something else needs to take care of deleting the object with delete.

louisfoster avatar Feb 10 '22 14:02 louisfoster

/cc @doodlewind

Brooooooklyn avatar Feb 10 '22 14:02 Brooooooklyn

I tried adding delete paragraph; to the end of the function and tried building again. I didn't notice any difference in the memory with my tests, too much memory still being used and not released. In fact I believe I was also incorrect earlier about encode not also having some kind of leak, but it could possibly be my code.

louisfoster avatar Feb 10 '22 17:02 louisfoster