Skip to content

[ffigen] Remove objc_retain from bindings#1888

Merged
liamappelbe merged 2 commits into
mainfrom
no_objc_retain
Jan 14, 2025
Merged

[ffigen] Remove objc_retain from bindings#1888
liamappelbe merged 2 commits into
mainfrom
no_objc_retain

Conversation

@liamappelbe

@liamappelbe liamappelbe commented Jan 14, 2025

Copy link
Copy Markdown
Contributor

objc_retain behaves oddly on physical iOS devices, in certain build configurations (release mode plus other flags), when ARC is enabled. Anything from argument reordering (stack corruption) to outright crashes. My hypothesis is that calling this function explicitly under ARC might be undefined behavior that release builds can exploit for optimizations.

A more roundabout way of incrementing the ref count, that is more explicitly supported by the compiler, is to use bridge casts. These convert between ARC pointers and ordinary pointers, optionally altering the ref count.

id foo;
objc_retain(foo);
// Becomes
(__bridge id)(__bridge_retained void*)(foo);

This expression converts foo to a void* while incrementing its ref count, then converts it back to an id without changing its ref count.

Note that we're still using objc_retainBlock throughout the bindings, because it works slightly differently, and can't simply be swapped for a bridge cast (if objc_retainBlock is passed a stack allocated block, it converts it to a heap allocated block).

Unrelated change: Deleted objective_c_bindings_generated.dart.m, which is an old unused file.

@github-actions

github-actions Bot commented Jan 14, 2025

Copy link
Copy Markdown

PR Health

API leaks ⚠️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
objective_c ObjCBlockBase
c._ObjCBlockImpl
_ObjCBlockDesc
_ObjCBlockRef
_ObjCObject
_Dart_FinalizableHandle
_ObjCProtocol
_ObjCSelector
_ObjCMethodDesc
_ObjCObjectRef
_NSZone

This check can be disabled by tagging the PR with skip-leaking-check.

License Headers ✔️
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/jni/lib/src/third_party/generated_bindings.dart
pkgs/objective_c/lib/src/ns_input_stream.dart

@coveralls

coveralls commented Jan 14, 2025

Copy link
Copy Markdown

Coverage Status

coverage: 87.712% (+0.02%) from 87.691%
when pulling 841bc83 on no_objc_retain
into 3832e4b on main.

@liamappelbe liamappelbe marked this pull request as ready for review January 14, 2025 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants