Bug 186436 - [Cocoa] Remove all uses of NSAutoreleasePool as part of preparation for ARC
Summary: [Cocoa] Remove all uses of NSAutoreleasePool as part of preparation for ARC
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Darin Adler
URL:
Keywords: InRadar
Depends on:
Blocks: 221107
  Show dependency treegraph
 
Reported: 2018-06-08 10:32 PDT by Darin Adler
Modified: 2021-01-28 14:58 PST (History)
7 users (show)

See Also:


Attachments
Patch (31.34 KB, patch)
2018-06-08 10:41 PDT, Darin Adler
no flags Details | Formatted Diff | Diff
Patch (45.01 KB, patch)
2018-06-08 15:21 PDT, Darin Adler
no flags Details | Formatted Diff | Diff
Patch (56.15 KB, patch)
2018-06-08 16:11 PDT, Darin Adler
no flags Details | Formatted Diff | Diff
Patch (56.09 KB, patch)
2018-06-08 22:25 PDT, Darin Adler
no flags Details | Formatted Diff | Diff
Sample Crash Log (54.16 KB, text/plain)
2018-06-11 10:42 PDT, Dawei Fenton (:realdawei)
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Darin Adler 2018-06-08 10:32:31 PDT
[Cocoa] Remove all uses of NSAutoreleasePool as part of preparation for ARC
Comment 1 Darin Adler 2018-06-08 10:41:23 PDT
Created attachment 342292 [details]
Patch
Comment 2 Anders Carlsson 2018-06-08 10:46:23 PDT
Comment on attachment 342292 [details]
Patch

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

> Source/WTF/wtf/AutodrainedPool.h:37
> +// This class allows non-Objective-C C++ code to create an autorelease pool.
> +// It should not be used in Objective-C++ code; instead @autoreleasepool should be used.
> +// It can be used in cross-platform code; will compile down to nothing for non-Cocoa platforms.

Can you enforce this by using #error if __OBJC__ is defined?

> Source/WTF/wtf/AutodrainedPoolMac.mm:37
> +    : m_pool(NSPushAutoreleasePool(0))

{ } initialization?

> Source/WebKitLegacy/mac/History/WebHistory.mm:601
> +    // FIXME: If we can test and see good performance draining the autorelease pool every time through the loop,
> +    // instead of once every 50 iterations, then we should switch to @autoreleasepool and get rid of the use of
> +    // FoundationSPI.h in this source file.

I'd go a step further and just use @autoreleasepool here - it should be fast enough and given that WebKit Legacy is deprecated I don't think it matters even if it's a little slower.

> Source/WebKitLegacy/mac/WebView/WebView.mm:7908
> +            // FIXME: If we can test and see good performance draining the autorelease pool every time
> +            // through the loop, instead of once every 10 iterations, then we should switch to
> +            // @autoreleasepool and get rid of the use of FoundationSPI.h in this source file.

Even more true here, since this is SPI that Safari no longer uses - not sure if anyone else does.
Comment 3 Darin Adler 2018-06-08 11:34:15 PDT
OK, I’ll fix the compilation errors and do what you suggested.
Comment 4 Darin Adler 2018-06-08 11:34:49 PDT
(In reply to Anders Carlsson from comment #2)
> Can you enforce this by using #error if __OBJC__ is defined?

The trick is to do that while allowing AutodrainedPoolMac.mm to compile. But I will do it.
Comment 5 Darin Adler 2018-06-08 15:21:19 PDT
Created attachment 342336 [details]
Patch
Comment 6 Anders Carlsson 2018-06-08 15:40:58 PDT
/Volumes/Data/EWS/WebKit/Tools/TestWebKitAPI/Tests/mac/StopLoadingFromDidFinishLoading.mm:51:21: error: use of undeclared identifier 'pool'; did you mean 'powl'?
    AutodrainedPool pool;
                    ^~~~
                    powl
Comment 7 Darin Adler 2018-06-08 16:05:08 PDT
Got a better version coming. Found another file using autorelease pool.
Comment 8 Darin Adler 2018-06-08 16:11:58 PDT
Created attachment 342342 [details]
Patch
Comment 9 EWS Watchlist 2018-06-08 16:14:04 PDT
Attachment 342342 [details] did not pass style-queue:


ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:47:  When wrapping a line, only indent 4 spaces.  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:48:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:49:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:51:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:52:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:53:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Source/WTF/wtf/spi/cocoa/FoundationSPI.h:36:  objc_autoreleasePoolPush is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/spi/cocoa/FoundationSPI.h:37:  objc_autoreleasePoolPop is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
Total errors found: 8 in 26 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 10 Darin Adler 2018-06-08 22:25:35 PDT
Created attachment 342360 [details]
Patch
Comment 11 EWS Watchlist 2018-06-08 22:27:52 PDT
Attachment 342360 [details] did not pass style-queue:


ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:47:  When wrapping a line, only indent 4 spaces.  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:48:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:49:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:51:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:52:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Tools/TestWebKitAPI/Tests/mac/MenuTypesForMouseEvents.mm:53:  Weird number of spaces at line-start.  Are you using a 4-space indent?  [whitespace/indent] [3]
ERROR: Source/WTF/wtf/spi/cocoa/FoundationSPI.h:36:  objc_autoreleasePoolPush is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/spi/cocoa/FoundationSPI.h:37:  objc_autoreleasePoolPop is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
Total errors found: 8 in 26 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 12 Darin Adler 2018-06-09 09:27:24 PDT
Committed r232660: <https://trac.webkit.org/changeset/232660>
Comment 13 Radar WebKit Bug Importer 2018-06-09 09:28:21 PDT
<rdar://problem/40969584>
Comment 14 Dawei Fenton (:realdawei) 2018-06-11 10:25:35 PDT
Looks like we are getting an API test failure after this patch

Sample output:
Test suite failed

Crashed

    TestWebKitAPI.WebKitLegacy.StopLoadingFromDidFinishLoading

https://build.webkit.org/builders/Apple%20High%20Sierra%20Release%20WK2%20(Tests)/builds/4986/steps/run-api-tests/logs/stdio


Excerpt from Crash log:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libobjc.A.dylib               	0x00007fff796bad3b objc_retain + 27
1   com.apple.WebKitLegacy        	0x000000010b0ea647 WebDocumentLoaderMac::setDataSource(WebDataSource*, WebView*) + 87 (WebDocumentLoaderMac.mm:62)
2   com.apple.WebKitLegacy        	0x000000010b100eb7 WebFrameLoaderClient::createDocumentLoader(WebCore::ResourceRequest const&, WebCore::SubstituteData const&) + 167 (WebFrameLoaderClient.mm:1353)
3   com.apple.WebCore             	0x000000010c3d99d7 WebCore::FrameLoader::load(WebCore::FrameLoadRequest&&) + 583 (FrameLoader.cpp:1454)
4   com.apple.WebKitLegacy        	0x000000010b0faf90 -[WebFrame _loadData:MIMEType:textEncodingName:baseURL:unreachableURL:] + 1248
5   com.apple.WebKitLegacy        	0x000000010b0fb439 -[WebFrame _loadHTMLString:baseURL:unreachableURL:] + 89 (WebFrame.mm:2548)
6   TestWebKitAPI                 	0x00000001084093fb TestWebKitAPI::WebKitLegacy_StopLoadingFromDidFinishLoading_Test::TestBody() + 197 (StopLoadingFromDidFinishLoading.mm:54)
7   TestWebKitAPI                 	0x00000001084e3958 testing::Test::Run() + 92
8   TestWebKitAPI                 	0x00000001084e41b4 testing::internal::TestInfoImpl::Run() + 180
9   TestWebKitAPI                 	0x00000001084e459c testing::TestCase::Run() + 196
10  TestWebKitAPI                 	0x00000001084e7d76 testing::internal::UnitTestImpl::RunAllTests() + 614
11  TestWebKitAPI                 	0x0000000108411bee TestWebKitAPI::TestsController::run(int, char**) + 120 (TestsController.cpp:86)
12  TestWebKitAPI                 	0x00000001084c37fb main + 344 (mainMac.mm:52)
13  libdyld.dylib                 	0x00007fff7a2df015 start + 1
Comment 15 Dawei Fenton (:realdawei) 2018-06-11 10:42:22 PDT
Created attachment 342443 [details]
Sample Crash Log
Comment 16 Darin Adler 2018-06-11 12:21:00 PDT
OK, fine to roll out if the test is failing. I can fix the problem and re-land later.
Comment 17 Darin Adler 2018-06-11 12:22:16 PDT
Oh, I see why it’s failing. The error is in the test, not WebCore and is really trivial to fix.
Comment 18 Darin Adler 2018-06-11 12:22:46 PDT
Feel free to roll out, or to just disable this one test. Either way I can easily quickly fix this once I get to a computer.
Comment 19 Darin Adler 2018-06-11 12:24:38 PDT
Here’s the fix for StopLoadingFromDidFinishLoading.mm; resourceLoadDelegate is not retained so it needs to be this:

-     webView.get().resourceLoadDelegate = adoptNS([[StopLoadingFromDidFinishLoadingDelegate alloc] init]).get();
+     auto resourceLoadDelegate = adoptNS([[StopLoadingFromDidFinishLoadingDelegate alloc] init]);
+     webView.get().resourceLoadDelegate = resourceLoadDelegate.get();

The spacing isn’t right on the change, but that will fix it.
Comment 20 Brady Eidson 2018-06-11 12:56:07 PDT
(In reply to Darin Adler from comment #19)
> Here’s the fix for StopLoadingFromDidFinishLoading.mm; resourceLoadDelegate
> is not retained so it needs to be this:
> 
> -     webView.get().resourceLoadDelegate =
> adoptNS([[StopLoadingFromDidFinishLoadingDelegate alloc] init]).get();
> +     auto resourceLoadDelegate =
> adoptNS([[StopLoadingFromDidFinishLoadingDelegate alloc] init]);
> +     webView.get().resourceLoadDelegate = resourceLoadDelegate.get();
> 
> The spacing isn’t right on the change, but that will fix it.

Followup landed in https://trac.webkit.org/changeset/232727/webkit
Comment 21 Dawei Fenton (:realdawei) 2018-06-11 13:00:15 PDT
(In reply to Brady Eidson from comment #20)
> (In reply to Darin Adler from comment #19)
> > Here’s the fix for StopLoadingFromDidFinishLoading.mm; resourceLoadDelegate
> > is not retained so it needs to be this:
> > 
> > -     webView.get().resourceLoadDelegate =
> > adoptNS([[StopLoadingFromDidFinishLoadingDelegate alloc] init]).get();
> > +     auto resourceLoadDelegate =
> > adoptNS([[StopLoadingFromDidFinishLoadingDelegate alloc] init]);
> > +     webView.get().resourceLoadDelegate = resourceLoadDelegate.get();
> > 
> > The spacing isn’t right on the change, but that will fix it.
> 
> Followup landed in https://trac.webkit.org/changeset/232727/webkit

great thanks!