[webkit-dev] Coding style change proposal: Avoid using Objective C autorelease when possible

Jean-Yves Avenard jean-yves.avenard at apple.com
Fri Apr 4 20:32:19 PDT 2025


+1

There’s quite a few of those, I’ve removed some when I encountered them, some of them not so trivial replacement due to local scoping. 

Jean-Yves

Sent from my iPhone

> On 5 Apr 2025, at 11:45 am, Chris Dumez via webkit-dev <webkit-dev at lists.webkit.org> wrote:
> 
> Hi,
> 
> I would like to propose updating the WebKit coding style to recommend avoiding autorelease in Objective C whenever possible, for the following reasons:
> - Performance reasons:
>   - Cleaning up objects from all over the memory space in an autorelease pool can be slow and can cause memory spikes.
> - Memory Safety:
>   - Holding a RetainPtr to the object makes lifetime clearer, whereas autorelease pool draining is not as clear and can result in use-after-free bugs.
> - Easier debugging:
>   - Memory corruption bugs are harder to debug when it happens during autorelease pool drain.
> 
> Concretely, this means writing code like this:
> ```
> RetainPtr extensionDatabaseQueueName = adoptNS([[NSString alloc] initWithFormat:@"com.apple.WebKit.WKWebExtensionSQLiteStore.%@", _uniqueIdentifier]); 
> String foo = “foo”_s;
> [object callWith:foo.createNSString().get()];
> URL foo = “https://www.webkit.org”_s;
> [object callWith:foo.createNSURL().get()];
> ```
> Instead of:
> ```
> NSString *extensionDatabaseQueueName = [NSString stringWithFormat:@"com.apple.WebKit.WKWebExtensionSQLiteStore.%@", _uniqueIdentifier];
> RetainPtr extensionDatabaseQueueName = [NSString stringWithFormat:@"com.apple.WebKit.WKWebExtensionSQLiteStore.%@", _uniqueIdentifier];
> String foo = “foo”_s;
> [object callWith:(NSString *)foo]; // This does autorelease internally.
> URL foo = “https://www.webkit.org”_s;
> [object callWith:(NSURL *)foo]; // This does autorelease internally.
> ```
> 
> Example PR: https://github.com/WebKit/WebKit/pull/43656
> 
> What do you think?
> 
> Thanks,
> Chris Dumez.
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> https://lists.webkit.org/mailman/listinfo/webkit-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20250405/27531b61/attachment.htm>


More information about the webkit-dev mailing list