Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ffigen] Remove objc_retain from bindings #1888

Merged
merged 2 commits into from
Jan 14, 2025
Merged

[ffigen] Remove objc_retain from bindings #1888

merged 2 commits into from
Jan 14, 2025

Conversation

liamappelbe
Copy link
Contributor

@liamappelbe liamappelbe commented Jan 14, 2025

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.

Copy link

github-actions bot commented Jan 14, 2025

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
Copy link

coveralls commented Jan 14, 2025

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
@liamappelbe liamappelbe merged commit 7c7cced into main Jan 14, 2025
24 checks passed
@liamappelbe liamappelbe deleted the no_objc_retain branch January 14, 2025 22:15
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