Title: Receiver-consuming calls: an abandoned receiver leaks, a substituted one is over-released · Issue #571 · NativeScript/ios · GitHub
Open Graph Title: Receiver-consuming calls: an abandoned receiver leaks, a substituted one is over-released · Issue #571 · NativeScript/ios
X Title: Receiver-consuming calls: an abandoned receiver leaks, a substituted one is over-released · Issue #571 · NativeScript/ios
Description: Summary Initializers and NS_REPLACES_RECEIVER methods (-awakeAfterUsingCoder:) consume their receiver: the caller hands over its reference, and the method returns an owned object in its place, usually the receiver itself. The runtime get...
Open Graph Description: Summary Initializers and NS_REPLACES_RECEIVER methods (-awakeAfterUsingCoder:) consume their receiver: the caller hands over its reference, and the method returns an owned object in its place, usua...
X Description: Summary Initializers and NS_REPLACES_RECEIVER methods (-awakeAfterUsingCoder:) consume their receiver: the caller hands over its reference, and the method returns an owned object in its place, usua...
Opengraph URL: https://github.com/NativeScript/ios/issues/571
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Receiver-consuming calls: an abandoned receiver leaks, a substituted one is over-released","articleBody":"## Summary\n\nInitializers and `NS_REPLACES_RECEIVER` methods (`-awakeAfterUsingCoder:`) consume their receiver: the caller hands over its reference, and the method returns an owned object in its place, usually the receiver itself. The runtime gets the receiver's side of that contract wrong in two ways. #569 fixed the result side and lists both of these as not fixed.\n\n## 1. A JS initializer that returns something else leaks its receiver\n\nWhen a JS-implemented initializer returns a different object, or nil, the object allocated for the call is never released:\n\n```js\nconst X = NSObject.extend({\n init() { return someOtherObject; }, // or: return null\n});\nX.new(); // the allocated receiver keeps its alloc +1\nnew X(); // same\nX.alloc().init(); // same\n```\n\nIn ObjC, `self = other; return self;` releases the original under ARC. A JS initializer has no way to, and `ArgConverter::MethodCallback` never releases the receiver either. It can't simply do so: when the JS initializer returns what `super.init…()` substituted, that super initializer already consumed the receiver, and releasing it again would free it under its wrapper. The two cases look the same from `MethodCallback`.\n\nA JS `-awakeAfterUsingCoder:` that returns another object leaves the caller's consumed reference behind in the same way.\n\nWith the wrapper lifetime work (#559 and up), the leftover retain reads as a native owner, so the abandoned receiver's wrapper stays a strong root.\n\n## 2. Consuming calls on a receiver that already existed\n\nFor a call from JS, the runtime passes the receiver without a reference for the callee to consume. That is fine while the method returns its receiver. When it doesn't:\n\n- **The receiver is over-released.** A native initializer that substitutes its receiver releases it, and the only reference there is belongs to the `Caches::Instances` entry. The wrapper is left pointing at an object that can be freed under it. This applies to re-initializing a wrapped object, to `super.init…()` inside a JS initializer, and to `Proto.initX.apply(this)`.\n- **A substitute that is already wrapped leaks a retain.** `Interop::ReleasesCachedOwnedReturn` never releases for a consumed receiver that existed before the call, because the usual result is the receiver itself, which carries no net +1. So when the result is a different, already wrapped object, its +1 stays. The likeliest case is `super.init…()` inside a JS initializer returning a cached singleton.\n\nReceivers allocated for the call (`X.alloc().init…()`, `new X()`) are not affected: there the alloc +1 is the reference the initializer consumes.\n\n## Suggested fix\n\n1. Before every receiver-consuming call from JS on a receiver that already existed, retain the receiver, so the callee has a reference to consume. Then every result of such a call is a real +1, whatever it is, and the cache-hit rule no longer needs its `Consumed` exception: release unless the result is not owned.\n2. For JS-implemented initializers, record on the call whether a consuming call was made on the receiver while the JS method ran (the `super.init…()` case). When the JS method returns something other than the receiver and no such call was made, release the receiver in `MethodCallback`.\n\nBoth parts change who owns the receiver across the JS boundary, so they need the same care as #569: exact counts for every caller and implementation, under AddressSanitizer.\n\n## Tests to add\n\n- A JS initializer returning another object, and one returning null, through `X.new()`, `new X()` and `X.alloc().init()`: the allocated receiver is freed exactly once.\n- A JS initializer returning the substitute of a super initializer: the receiver is freed exactly once and not twice (a fixture for a substituting initializer exists: `TNSSubstitutingInit`).\n- Re-initializing a wrapped object with a native initializer that substitutes it: the original is not freed while its wrapper is live, and the substitute's count is balanced when it is already wrapped.\n- `super.init…()` inside a JS initializer returning an already wrapped object: no retain is left on it.\n- A JS `-awakeAfterUsingCoder:` returning another object: the receiver's count is balanced.\n\nThese come from tracing the code paths for #569; the receiver leak in part 1 is also visible in that PR's specs, where the abandoned receiver is never freed.\n","author":{"url":"https://github.com/edusperoni","@type":"Person","name":"edusperoni"},"datePublished":"2026-11-15T16:12:08Z","url":"https://github.com/NativeScript/ios/issues/571"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d7d55177-0f8f-a8fc-1122-07c238401b4e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B878:1D86C4:148BAD8:1A1DBFF:6B012B39 |
| html-safe-nonce | c871ac633f50c8a2c14793ded0720e6f51515dcd3b2adc48f021fe2972dfe5fd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCODc4OjFEODZDNDoxNDhCQUQ4OjFBMURCRkY6NkIwMTJCMzkiLCJ2aXNpdG9yX2lkIjoiMTU2MzUwMTY2MjQzMjIwOTcyMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6048649cda2b95490601c90f7baca018666e6758b50baa5ddc1b1fe1934c66aa |
| hovercard-subject-tag | issue:6406314565 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/NativeScript/ios/571/issue_layout |
| twitter:image | https://opengraph.githubassets.com/475c01abcd59b7d979db2e743a2e76de7bd8687cb1a2c957002e311341556721/NativeScript/ios/issues/571 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/475c01abcd59b7d979db2e743a2e76de7bd8687cb1a2c957002e311341556721/NativeScript/ios/issues/571 |
| og:image:alt | Summary Initializers and NS_REPLACES_RECEIVER methods (-awakeAfterUsingCoder:) consume their receiver: the caller hands over its reference, and the method returns an owned object in its place, usua... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | edusperoni |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/NativeScript/ios git https://github.com/NativeScript/ios.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 172934359 |
| octolytics-dimension-repository_nwo | NativeScript/ios |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 172934359 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/ios |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width