Bug 76219

Summary: cairo_t* memory leak in GraphicsContext::platformInit
Product: WebKit Reporter: Brian Stuart <bstuart>
Component: WebCore Misc.Assignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: bfulgham, dbates, hirano-txb, mrobinson, webkit.review.bot, yoshida-hxa
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: PC   
OS: Windows 7   
URL: http://smithmicro.com
Attachments:
Description Flags
Proposal for fixing the memory leak in WinCairo
none
Proposal for fixing the memory leak in WinCairo (Revised patch)
bfulgham: review-
Proposal for fixing the memory leak in WinCairo (Revised patch v2) bfulgham: review+, bfulgham: commit-queue-

Description Brian Stuart 2012-01-12 15:06:58 PST
In source\webcore\platform\graphics\win\graphicscontextcairowin.cpp GraphicsContext::platformInit createCairoContextWithHDC is called returning a cairo_t* which has a reference count of 1. Passing that cairo_t* to PlatformContextCairo's ctor causes the reference count to increment to 2. Now when ~PlatformContextCairo is called it's cairo_t* is leaked.

Here is my workaround, a better solution would involve using adoptRef and passing a RefPtr to PlatformContextCairo's ctor:

void GraphicsContext::platformInit(HDC dc, bool hasAlpha)
{
    cairo_t* cr = 0;
    if (dc)
        cr = createCairoContextWithHDC(dc, hasAlpha);//cr is created with ref count of 1
    else
        setPaintingDisabled(true);	
	
    m_data = new GraphicsContextPlatformPrivateToplevel(new PlatformContextCairo(cr));//cr now has a ref count of 2 because of PlatformContextCairo RefPtr<cairo_t> m_cr;
	
    cairo_destroy(cr);//to prevent memory leak restore cr ref count to 1 so ~PlatformContextCairo will destroy with ref count of 1 
	
	m_data->m_hdc = dc;
    if (platformContext()->cr()) {
        // Make sure the context starts in sync with our state.
        setPlatformFillColor(fillColor(), fillColorSpace());
        setPlatformStrokeColor(strokeColor(), strokeColorSpace());
    }
}
Comment 1 Hideki 2012-10-17 00:21:17 PDT
Created attachment 169104 [details]
Proposal for fixing the memory leak in WinCairo

Thank you for useful information, Brain.

My team also found same memory leak problem in WinCairo port and
figured out the same bug.

Here is the patch which we made.

We just moved
 cairo_destroy(cr);
to the end of the function and added if statement to 
make sure that cr is not ZERO for fear that createCairoContextWithHDC
is not called.
Comment 2 WebKit Review Bot 2012-11-05 18:59:50 PST
Attachment 169104 [details] did not pass style-queue:

Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'Source/WebCore/platform/graphics/win/Graph..." exit_code: 1
Source/WebCore/platform/graphics/win/GraphicsContextCairoWin.cpp:91:  More than one command on the same line in if  [whitespace/parens] [4]
Total errors found: 1 in 1 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Hideki 2012-11-11 23:12:18 PST
Created attachment 173567 [details]
Proposal for fixing the memory leak in WinCairo (Revised patch)

We revised the patch refering the comment sent by BOT.
We just modified its format, no context of program has been changed.

------
Failed to run "['Tools/Scripts/check-webkit-style', '--diff-files', u'Source/WebCore/platform/graphics/win/Graph..." exit_code: 1
Source/WebCore/platform/graphics/win/GraphicsContextCairoWin.cpp:91:  More than one command on the same line in if  [whitespace/parens] [4]
Total errors found: 1 in 1 files
------
Comment 4 Brent Fulgham 2012-11-13 09:20:20 PST
Comment on attachment 173567 [details]
Proposal for fixing the memory leak in WinCairo (Revised patch)

Nice catch.  I think this patch is fine, though there is no ChangeLog entry.  Please add a ChangeLog to the patch and I will be happy to approve it.  r- because of the missing ChangeLog.
Comment 5 Hideki 2012-11-14 00:50:59 PST
Created attachment 174097 [details]
Proposal for fixing the memory leak in WinCairo (Revised patch v2)

Added ChangeLog.
Comment 6 Brent Fulgham 2012-11-14 09:29:15 PST
Comment on attachment 174097 [details]
Proposal for fixing the memory leak in WinCairo (Revised patch v2)

View in context: https://bugs.webkit.org/attachment.cgi?id=174097&action=review

r = me; I will correct the URL ordering and land the change.

> Source/WebCore/ChangeLog:3
> +        https://bugs.webkit.org/show_bug.cgi?id=76219

We put the bug URL after the short description.
Comment 7 Brent Fulgham 2012-11-14 10:54:06 PST
Committed r134629: <http://trac.webkit.org/changeset/134629>