Bug 67815 - [Chromium] Fix leak of Skia stream with custom CSS fonts
Summary: [Chromium] Fix leak of Skia stream with custom CSS fonts
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: James Simonsen
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-08 16:09 PDT by James Simonsen
Modified: 2011-09-09 11:30 PDT (History)
4 users (show)

See Also:


Attachments
Patch (2.67 KB, patch)
2011-09-08 16:11 PDT, James Simonsen
no flags Details | Formatted Diff | Diff
Patch (2.30 KB, patch)
2011-09-08 16:59 PDT, James Simonsen
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description James Simonsen 2011-09-08 16:09:55 PDT
[Chromium] Fix leak of Skia stream with custom CSS fonts
Comment 1 James Simonsen 2011-09-08 16:11:06 PDT
Created attachment 106809 [details]
Patch
Comment 2 James Simonsen 2011-09-08 16:19:36 PDT
FYI, the underlying SKIA function is here:

http://google.com/codesearch#OAMlx_jo-ck/src/skia/ext/SkFontHost_fontconfig.cpp&l=166

It clearly doesn't take ownership of the stream, so we have to delete it.
Comment 3 Tony Chang 2011-09-08 16:38:00 PDT
Do we use SkFontHost_fontconfig on mac?

Also, it looks like in the original version upstream, the stream is owned by the SkTypeface:
http://codesearch.google.com/#OAMlx_jo-ck/src/third_party/skia/src/ports/SkFontHost_linux.cpp&exact_package=chromium&q=file:.*skfonthost.*linux.*&type=cs&l=579

Maybe we need to fix src/skia/ext/SkFontHost_fontconfig.cpp to delete it?
Comment 4 Adam Barth 2011-09-08 16:41:44 PDT
Comment on attachment 106809 [details]
Patch

Oops.  Didn't see Tony's comment.
Comment 5 James Simonsen 2011-09-08 16:58:30 PDT
(In reply to comment #3)
> Do we use SkFontHost_fontconfig on mac?

I didn't look. The original bug said Mac had the same leak. I assumed it was implemented the same way. Turns out the two Mac implementations don't do anything with the stream, so that's why they leak it.

> Also, it looks like in the original version upstream, the stream is owned by the SkTypeface:
> http://codesearch.google.com/#OAMlx_jo-ck/src/third_party/skia/src/ports/SkFontHost_linux.cpp&exact_package=chromium&q=file:.*skfonthost.*linux.*&type=cs&l=579
> 
> Maybe we need to fix src/skia/ext/SkFontHost_fontconfig.cpp to delete it?

Wow, this is really inconsistent. It looks like the _linux one uses ref counting. We can do the same thing here and then not have to worry about how each platform deals with it.

New patch coming.
Comment 6 James Simonsen 2011-09-08 16:59:24 PDT
Created attachment 106814 [details]
Patch
Comment 7 Adam Barth 2011-09-08 17:07:03 PDT
Comment on attachment 106814 [details]
Patch

Please don't call unref directly.  Maybe you should use a RefPtr and adoptRef?
Comment 8 Adam Barth 2011-09-08 17:07:50 PDT
Comment on attachment 106814 [details]
Patch

If RemoteFontStream is RefCounted, its constructor should be private and it should expose a ::create method that calls adoptRef.
Comment 9 James Simonsen 2011-09-08 17:12:22 PDT
(In reply to comment #7)
> (From update of attachment 106814 [details])
> Please don't call unref directly.  Maybe you should use a RefPtr and adoptRef?

This is a SkRefCnt object. I'm honestly not familiar at all with Skia. Is there a RefPtr equivalent for them?
Comment 10 Adam Barth 2011-09-08 17:17:00 PDT
> This is a SkRefCnt object. I'm honestly not familiar at all with Skia. Is there a RefPtr equivalent for them?

I'm not sure about Skia, but we've created specialized RefPtr-like classes to handle these cases in the past.  For example, we have one that understands how to deal with CoreGraphics ref-counted pointers.
Comment 12 Adam Barth 2011-09-08 17:19:03 PDT
We probably need something similar to that for Skia if Skia doesn't have one already for us to use.
Comment 13 James Simonsen 2011-09-08 17:54:18 PDT
(In reply to comment #12)
> We probably need something similar to that for Skia if Skia doesn't have one already for us to use.

They have one, but it's broken. Both SkRefCnt's constructor and SkRefPtr's constructor call ref() on the object, so you immediately have a ref count of 2 and must manually unref() once before SkRefPtr works as expected. See this in action here:

http://skia.googlecode.com/svn-history/r1812/trunk/tests/PDFPrimitivesTest.cpp
...
SkRefPtr<SkPDFInt> int42 = new SkPDFInt(42);
int42->unref();  // SkRefPtr and new both took a reference.
...

That's probably why we don't use SkRefPtr at all in WebKit. Instead, it looks like people just call unref() directly (there are 6 other references in this directory). I vote for just following them and moving on. We can file a separate bug to fix all of the callers of Skia's unref().
Comment 14 Adam Barth 2011-09-08 18:04:59 PDT
That API sounds amazingly error prone.  Sounds like they need to add the concept of PassRefPtr or adoptRef.  Perhaps we should create a subclass of SkRefPtr that add an adoptRef method?

If you feel like this is too big a yak to shave, you should feel free to put it off to a future patch.
Comment 15 James Simonsen 2011-09-08 18:39:27 PDT
(In reply to comment #14)
> If you feel like this is too big a yak to shave, you should feel free to put it off to a future patch.

Honestly, I think the right thing to do would be to fix SkRefPtr to behave like every other RefPtr-like class in the world. I already found one place in Chrome where we use SkRefPtr "incorrectly" and leak something. Once SkRefPtr is fixed, then we can just use it as normal in WebKit.

For now, I'd like to just land this patch, because it's on the Chrome CodeYellow list.
Comment 16 James Simonsen 2011-09-08 18:50:40 PDT
(In reply to comment #15)
> Honestly, I think the right thing to do would be to fix SkRefPtr to behave like every other RefPtr-like class in the world. I already found one place in Chrome where we use SkRefPtr "incorrectly" and leak something. Once SkRefPtr is fixed, then we can just use it as normal in WebKit.

FYI, I submitted a bug against Skia about this. I'll fix it if nobody else does.
Comment 17 Adam Barth 2011-09-08 19:04:22 PDT
Comment on attachment 106814 [details]
Patch

Sounds like a good plan.
Comment 18 WebKit Review Bot 2011-09-09 00:13:08 PDT
Comment on attachment 106814 [details]
Patch

Clearing flags on attachment: 106814

Committed r94838: <http://trac.webkit.org/changeset/94838>
Comment 19 WebKit Review Bot 2011-09-09 00:13:13 PDT
All reviewed patches have been landed.  Closing bug.
Comment 20 Mike Reed 2011-09-09 05:18:58 PDT
Just saw this thread this morning...

Another fix that is consistent with other code in skia could be:

SkAutoTUnref<SkStream> stream = new RemoteFontStream(buffer);
SkTypeface* typeface = SkTypeface::CreateFromStream(stream.get());
Comment 21 Adam Barth 2011-09-09 11:30:08 PDT
Thanks Mike.  Maybe we should use that pattern everywhere so that folks will have good examples to emulate.