[webkit-dev] Coding style change proposal: Avoid using Objective C autorelease when possible
Chris Dumez
cdumez at apple.com
Fri Apr 4 17:44:58 PDT 2025
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20250404/f9cc3918/attachment.htm>
More information about the webkit-dev
mailing list