<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[23957] trunk/WebKit</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/23957">23957</a></dd>
<dt>Author</dt> <dd>darin</dd>
<dt>Date</dt> <dd>2007-07-03 15:02:27 -0700 (Tue, 03 Jul 2007)</dd>
</dl>

<h3>Log Message</h3>
<pre>        Reviewed by Maciej.

        - fix &lt;rdar://problem/5310848&gt; WebDataSource lifetime problem -- may be cause of the leaks seen on the buildbot

        * WebView/WebDataSource.mm:
        (-[WebDataSourcePrivate dealloc]): Added a call to the new detachDataSource function.
        (-[WebDataSourcePrivate finalize]): Ditto.

        * WebView/WebDocumentLoaderMac.h: Added detachDataSource function to be used when the
        WebDataSource is deallocated. Added retain/releaseDataSource helper functions to be
        used to retain and release the data source object. Replaced the m_hasEverBeenDetached
        boolean with a more primitive and hence easier to understand m_isDataSourceRetained boolean.

        * WebView/WebDocumentLoaderMac.mm:
        (WebDocumentLoaderMac::WebDocumentLoaderMac): Initialize m_isDataSourceRetained to false.
        (WebDocumentLoaderMac::setDataSource): Call retainDataSource instead of calling HardRetain
        on the dataSource parameter. Also updated a comment.
        (WebDocumentLoaderMac::attachToFrame): Call retainDataSource unconditionally rather than
        trying to use m_hasEverBeenDetached to decide if a retain is needed. Also got rid of an
        assertion that m_loadingResources is empty -- not important any more.
        (WebDocumentLoaderMac::detachFromFrame): Call releaseDataSource instead of using
        HardRelease, but only if m_loadingResources is empty. If it's non-empty, then we'll
        do the releaseDataSource later in decreaseLoadCount.
        (WebDocumentLoaderMac::increaseLoadCount): Call retainDataSource unconditionally
        rather than calling HardRetain only if the old set of resources was empty.
        (WebDocumentLoaderMac::decreaseLoadCount): Call releaseDataSource if m_loadingResources
        is empty and we're not attached to a frame. If we are attached to a frame, then
        we'll do the releaseDataSource later in detachFromFrame.
        (WebDocumentLoaderMac::retainDataSource): Added. Calls CFRetain, but only if the data
        source is not already retained (according to the boolean).
        (WebDocumentLoaderMac::releaseDataSource): Added. Calls CFRelease, but only if the data
        source is currently retained (according to the boolean).
        (WebDocumentLoaderMac::detachDataSource): Added. Sets m_dataSource to nil. Since this
        is only called from WebDataSource's dealloc and finalize methods, it won't ever be called
        when the m_isDataSourceRetained boolean is true.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkWebKitChangeLog">trunk/WebKit/ChangeLog</a></li>
<li><a href="#trunkWebKitWebViewWebDataSourcemm">trunk/WebKit/WebView/WebDataSource.mm</a></li>
<li><a href="#trunkWebKitWebViewWebDocumentLoaderMach">trunk/WebKit/WebView/WebDocumentLoaderMac.h</a></li>
<li><a href="#trunkWebKitWebViewWebDocumentLoaderMacmm">trunk/WebKit/WebView/WebDocumentLoaderMac.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/WebKit/ChangeLog (23956 => 23957)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKit/ChangeLog        2007-07-03 22:01:29 UTC (rev 23956)
+++ trunk/WebKit/ChangeLog        2007-07-03 22:02:27 UTC (rev 23957)
</span><span class="lines">@@ -1,5 +1,43 @@
</span><span class="cx"> 2007-07-03  Darin Adler  &lt;darin@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Reviewed by Maciej.
+
+        - fix &lt;rdar://problem/5310848&gt; WebDataSource lifetime problem -- may be cause of the leaks seen on the buildbot
+
+        * WebView/WebDataSource.mm:
+        (-[WebDataSourcePrivate dealloc]): Added a call to the new detachDataSource function.
+        (-[WebDataSourcePrivate finalize]): Ditto.
+
+        * WebView/WebDocumentLoaderMac.h: Added detachDataSource function to be used when the
+        WebDataSource is deallocated. Added retain/releaseDataSource helper functions to be
+        used to retain and release the data source object. Replaced the m_hasEverBeenDetached
+        boolean with a more primitive and hence easier to understand m_isDataSourceRetained boolean.
+
+        * WebView/WebDocumentLoaderMac.mm:
+        (WebDocumentLoaderMac::WebDocumentLoaderMac): Initialize m_isDataSourceRetained to false.
+        (WebDocumentLoaderMac::setDataSource): Call retainDataSource instead of calling HardRetain
+        on the dataSource parameter. Also updated a comment.
+        (WebDocumentLoaderMac::attachToFrame): Call retainDataSource unconditionally rather than
+        trying to use m_hasEverBeenDetached to decide if a retain is needed. Also got rid of an
+        assertion that m_loadingResources is empty -- not important any more.
+        (WebDocumentLoaderMac::detachFromFrame): Call releaseDataSource instead of using
+        HardRelease, but only if m_loadingResources is empty. If it's non-empty, then we'll
+        do the releaseDataSource later in decreaseLoadCount.
+        (WebDocumentLoaderMac::increaseLoadCount): Call retainDataSource unconditionally
+        rather than calling HardRetain only if the old set of resources was empty.
+        (WebDocumentLoaderMac::decreaseLoadCount): Call releaseDataSource if m_loadingResources
+        is empty and we're not attached to a frame. If we are attached to a frame, then
+        we'll do the releaseDataSource later in detachFromFrame.
+        (WebDocumentLoaderMac::retainDataSource): Added. Calls CFRetain, but only if the data
+        source is not already retained (according to the boolean).
+        (WebDocumentLoaderMac::releaseDataSource): Added. Calls CFRelease, but only if the data
+        source is currently retained (according to the boolean).
+        (WebDocumentLoaderMac::detachDataSource): Added. Sets m_dataSource to nil. Since this
+        is only called from WebDataSource's dealloc and finalize methods, it won't ever be called
+        when the m_isDataSourceRetained boolean is true.
+
+2007-07-03  Darin Adler  &lt;darin@apple.com&gt;
+
</ins><span class="cx">         - forgot to check in one file in the fix for &lt;rdar://problem/5307880&gt;
</span><span class="cx">           some classes need finalize methods because of non-trivial work done in dealloc methods
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkWebKitWebViewWebDataSourcemm"></a>
<div class="modfile"><h4>Modified: trunk/WebKit/WebView/WebDataSource.mm (23956 => 23957)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKit/WebView/WebDataSource.mm        2007-07-03 22:01:29 UTC (rev 23956)
+++ trunk/WebKit/WebView/WebDataSource.mm        2007-07-03 22:02:27 UTC (rev 23957)
</span><span class="lines">@@ -61,18 +61,15 @@
</span><span class="cx"> 
</span><span class="cx"> using namespace WebCore;
</span><span class="cx"> 
</span><del>-@interface WebDataSourcePrivate : NSObject
-{
-    @public
-    
-    WebDocumentLoaderMac *loader;
</del><ins>+@interface WebDataSourcePrivate : NSObject {
+@public
+    WebDocumentLoaderMac* loader;
</ins><span class="cx">    
</span><span class="cx">     id &lt;WebDocumentRepresentation&gt; representation;
</span><span class="cx">     
</span><span class="cx">     WebUnarchivingState *unarchivingState;
</span><span class="cx">     BOOL representationFinishedLoading;
</span><span class="cx"> }
</span><del>-
</del><span class="cx"> @end
</span><span class="cx"> 
</span><span class="cx"> @implementation WebDataSourcePrivate 
</span><span class="lines">@@ -87,7 +84,7 @@
</span><span class="cx"> - (void)dealloc
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!loader-&gt;isLoading());
</span><del>-
</del><ins>+    loader-&gt;detachDataSource();
</ins><span class="cx">     loader-&gt;deref();
</span><span class="cx">     
</span><span class="cx">     [representation release];
</span><span class="lines">@@ -98,8 +95,10 @@
</span><span class="cx"> 
</span><span class="cx"> - (void)finalize
</span><span class="cx"> {
</span><del>-    ASSERT(!loader-&gt;isLoading());
</del><ins>+    ASSERT_MAIN_THREAD();
</ins><span class="cx"> 
</span><ins>+    ASSERT(!loader-&gt;isLoading());
+    loader-&gt;detachDataSource();
</ins><span class="cx">     loader-&gt;deref();
</span><span class="cx"> 
</span><span class="cx">     [super finalize];
</span></span></pre></div>
<a id="trunkWebKitWebViewWebDocumentLoaderMach"></a>
<div class="modfile"><h4>Modified: trunk/WebKit/WebView/WebDocumentLoaderMac.h (23956 => 23957)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKit/WebView/WebDocumentLoaderMac.h        2007-07-03 22:01:29 UTC (rev 23956)
+++ trunk/WebKit/WebView/WebDocumentLoaderMac.h        2007-07-03 22:02:27 UTC (rev 23957)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
</del><ins>+ * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -37,12 +37,12 @@
</span><span class="cx">     class ResourceRequest;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-class WebDocumentLoaderMac : public WebCore::DocumentLoader
-{
</del><ins>+class WebDocumentLoaderMac : public WebCore::DocumentLoader {
</ins><span class="cx"> public:
</span><span class="cx">     WebDocumentLoaderMac(const WebCore::ResourceRequest&amp;, const WebCore::SubstituteData&amp;);
</span><span class="cx"> 
</span><span class="cx">     void setDataSource(WebDataSource *, WebView*);
</span><ins>+    void detachDataSource();
</ins><span class="cx">     WebDataSource *dataSource() const;
</span><span class="cx"> 
</span><span class="cx">     virtual void attachToFrame();
</span><span class="lines">@@ -50,10 +50,14 @@
</span><span class="cx"> 
</span><span class="cx">     void increaseLoadCount(unsigned long identifier);
</span><span class="cx">     void decreaseLoadCount(unsigned long identifier);
</span><ins>+
</ins><span class="cx"> private:
</span><ins>+    void retainDataSource();
+    void releaseDataSource();
+
</ins><span class="cx">     WebDataSource *m_dataSource;
</span><ins>+    bool m_isDataSourceRetained;
</ins><span class="cx">     RetainPtr&lt;id&gt; m_resourceLoadDelegate;
</span><span class="cx">     RetainPtr&lt;id&gt; m_downloadDelegate;
</span><del>-    bool m_hasEverBeenDetached;
</del><span class="cx">     HashSet&lt;unsigned long&gt; m_loadingResources;
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkWebKitWebViewWebDocumentLoaderMacmm"></a>
<div class="modfile"><h4>Modified: trunk/WebKit/WebView/WebDocumentLoaderMac.mm (23956 => 23957)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebKit/WebView/WebDocumentLoaderMac.mm        2007-07-03 22:01:29 UTC (rev 23956)
+++ trunk/WebKit/WebView/WebDocumentLoaderMac.mm        2007-07-03 22:02:27 UTC (rev 23957)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
</del><ins>+ * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -28,10 +28,6 @@
</span><span class="cx"> 
</span><span class="cx"> #import &quot;WebDocumentLoaderMac.h&quot;
</span><span class="cx"> 
</span><del>-#import &lt;JavaScriptCore/Assertions.h&gt;
-#import &lt;WebCore/SubstituteData.h&gt;
-#import &lt;WebCore/FoundationExtras.h&gt;
-
</del><span class="cx"> #import &quot;WebView.h&quot;
</span><span class="cx"> 
</span><span class="cx"> using namespace WebCore;
</span><span class="lines">@@ -39,7 +35,7 @@
</span><span class="cx"> WebDocumentLoaderMac::WebDocumentLoaderMac(const ResourceRequest&amp; request, const SubstituteData&amp; substituteData)
</span><span class="cx">     : DocumentLoader(request, substituteData)
</span><span class="cx">     , m_dataSource(nil)
</span><del>-    , m_hasEverBeenDetached(false)
</del><ins>+    , m_isDataSourceRetained(false)
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -60,13 +56,17 @@
</span><span class="cx"> void WebDocumentLoaderMac::setDataSource(WebDataSource *dataSource, WebView *webView)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!m_dataSource);
</span><del>-    HardRetain(dataSource);
</del><ins>+    ASSERT(!m_isDataSourceRetained);
+
</ins><span class="cx">     m_dataSource = dataSource;
</span><del>-    
</del><ins>+    retainDataSource();
+
</ins><span class="cx">     m_resourceLoadDelegate = [webView resourceLoadDelegate];
</span><span class="cx">     m_downloadDelegate = [webView downloadDelegate];
</span><span class="cx">     
</span><del>-    // Possibly work around a bug in Tiger AppKit where timers do not fire sometimes
</del><ins>+    // Work around a bug in Tiger AppKit's use of WebKit. The particular idiom used
+    // won't allow the timer to run, so deferring the main resource load with a timer
+    // causes a delay until something else wakes the run loop.
</ins><span class="cx">     // See &lt;rdar://problem/5266289&gt;
</span><span class="cx">     if (needsAppKitWorkaround(webView))
</span><span class="cx">         m_deferMainResourceDataLoad = false;
</span><span class="lines">@@ -80,42 +80,64 @@
</span><span class="cx"> void WebDocumentLoaderMac::attachToFrame()
</span><span class="cx"> {
</span><span class="cx">     DocumentLoader::attachToFrame();
</span><del>-    ASSERT(m_loadingResources.isEmpty());
</del><span class="cx"> 
</span><del>-    if (m_hasEverBeenDetached)
-        HardRetain(m_dataSource);
</del><ins>+    retainDataSource();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebDocumentLoaderMac::detachFromFrame()
</span><span class="cx"> {
</span><span class="cx">     DocumentLoader::detachFromFrame();
</span><del>-  
-    m_hasEverBeenDetached = true;
-    HardRelease(m_dataSource);
</del><ins>+
+    if (m_loadingResources.isEmpty())
+        releaseDataSource();
+
+    // FIXME: What prevents the data source from getting deallocated while the
+    // frame is not attached?
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebDocumentLoaderMac::increaseLoadCount(unsigned long identifier)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(m_dataSource);
</span><del>-    
</del><ins>+
</ins><span class="cx">     if (m_loadingResources.contains(identifier))
</span><span class="cx">         return;
</span><ins>+    m_loadingResources.add(identifier);
</ins><span class="cx"> 
</span><del>-    if (m_loadingResources.isEmpty())
-        HardRetain(m_dataSource);
-
-    m_loadingResources.add(identifier);
</del><ins>+    retainDataSource();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebDocumentLoaderMac::decreaseLoadCount(unsigned long identifier)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(m_loadingResources.contains(identifier));
</span><del>-    
</del><span class="cx">     m_loadingResources.remove(identifier);
</span><span class="cx">     
</span><span class="cx">     if (m_loadingResources.isEmpty()) {
</span><span class="cx">         m_resourceLoadDelegate = 0;
</span><span class="cx">         m_downloadDelegate = 0;
</span><del>-        HardRelease(m_dataSource);
</del><ins>+        if (!frame())
+            releaseDataSource();
</ins><span class="cx">     }
</span><span class="cx"> }
</span><ins>+
+void WebDocumentLoaderMac::retainDataSource()
+{
+    if (m_isDataSourceRetained || !m_dataSource)
+        return;
+    m_isDataSourceRetained = true;
+    CFRetain(m_dataSource);
+}
+
+void WebDocumentLoaderMac::releaseDataSource()
+{
+    if (!m_isDataSourceRetained)
+        return;
+    ASSERT(m_dataSource);
+    m_isDataSourceRetained = false;
+    CFRelease(m_dataSource);
+}
+
+void WebDocumentLoaderMac::detachDataSource()
+{
+    ASSERT(!m_isDataSourceRetained);
+    m_dataSource = nil;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>