DioxusLabs / blitz

A radically modular HTML/CSS rendering engine
Apache License 2.0
2.13k stars 42 forks source link

Fix a memory leak with Parley fonts #115

Closed DavidHusicka closed 2 weeks ago

DavidHusicka commented 3 months ago

Using a self-referencing struct and bunch of unsafe, we are able to extend the lifetime of the font names and their array without leaking them. These are necessary for a TextStyle which is part of Parley. This creates a shim called "OwnedTextStyle" that owns the data.

nicoburns commented 3 months ago

Using a self-referencing struct and bunch of unsafe, we are able to extend the lifetime of the font names and their array without leaking them. These are necessary for a TextStyle which is part of Parley. This creates a shim called "OwnedTextStyle" that owns the data.

Hmm... all the unsafe code with tricky lifetimes doesn't seem ideal. How about instead of a self-referencing struct, the names are stored in a document-global HashSet (or similar- perhaps a HashSet wouldn't work). There are unlikely to be many unique font names used within a single Document so this shouldn't use too much memory.

Or even a short-lived datastructure (that is passed in to the stylo_to_parley function so it is high enough up the stack for the lifetimes to work. I believe the FamilyNames style only needs to live until it is used (the reference is not stored).

nicoburns commented 2 weeks ago

This has now been fixed as part of the upgrade to Parley 0.2 which allows owned styles to be used.