fix(🐛): ref the typeface borrowed from SkFont - #4025
Open
dennytosp wants to merge 1 commit into
Open
Conversation
SkFont has two accessors, and getTypeface() is the borrowing one: /** Does not alter SkTypeface SkRefCnt. */ SkTypeface* getTypeface() const; /** Increases SkTypeface SkRefCnt by one. */ sk_sp<SkTypeface> refTypeface() const; sk_sp's raw-pointer constructor adopts without ref'ing, so wrapping getTypeface() handed the JsiSkTypeface wrapper a reference it never owned: every call cost the typeface one reference the moment that wrapper was collected. Code that derives paint variants from a loaded font calls this per render, and once the count crosses the typeface's real refcount it is freed while live SkFonts still point at it - crashing in SkTypeface::getBounds during a draw or in textToGlyphs during measurement. Fixes Shopify#3983
dennytosp
force-pushed
the
fix/ref-typeface-borrowed-from-font
branch
from
August 24, 2026 15:10
676d6bf to
fb04383
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3983.
SkFontexposes two accessors, and the one used here is the borrowing one — quotinginclude/core/SkFont.hon the pinnedchrome/m152:sk_sp's raw-pointer constructor adopts without ref'ing ("No call to ref() or unref() will be made"), sosk_sp<SkTypeface>(getObject()->getTypeface())handed theJsiSkTypefacewrapper a reference it never owned. Everyfont.getTypeface()call from JS therefore cost the typeface one reference the moment its wrapper was garbage collected. Code that derives a paint variant from a loaded font does this per render:Once the stolen unrefs cross the typeface's real refcount it is destroyed while live
SkFonts still point at it, which is where the crashes in the issue come from —SkTypeface::getBoundsunderGlyphsCmd::draw, andSkTypeface::textToGlyphsunderJsiSkFont::getGlyphIDs.refTypeface()is the accessor that hands over an owned reference, which is what the wrapper needs. It is also the only site incpp/that built ansk_spfrom a borrowed getter — I grepped the othersk_sp<Sk…>(…)constructions and they all adopt a freshlynew-ed object.The Web binding was never affected:
JsiSkFont.getTypefacethere wraps the CanvasKit handle, which is not refcounted from JS.