Title: isImplementedInClass leaks the losing sample instance on re-entrant or racing cache population · Issue #459 · NativeScript/ios · GitHub
Open Graph Title: isImplementedInClass leaks the losing sample instance on re-entrant or racing cache population · Issue #459 · NativeScript/ios
X Title: isImplementedInClass leaks the losing sample instance on re-entrant or racing cache population · Issue #459 · NativeScript/ios
Description: Problem MethodMeta::isImplementedInClass (NativeScript/runtime/Metadata.mm) keeps a process-wide cache of [klass alloc]-created sample instances used to answer respondsToSelector: for classes whose alloc returns a different class or whic...
Open Graph Description: Problem MethodMeta::isImplementedInClass (NativeScript/runtime/Metadata.mm) keeps a process-wide cache of [klass alloc]-created sample instances used to answer respondsToSelector: for classes whose...
X Description: Problem MethodMeta::isImplementedInClass (NativeScript/runtime/Metadata.mm) keeps a process-wide cache of [klass alloc]-created sample instances used to answer respondsToSelector: for classes whose...
Opengraph URL: https://github.com/NativeScript/ios/issues/459
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"isImplementedInClass leaks the losing sample instance on re-entrant or racing cache population","articleBody":"## Problem\n\n`MethodMeta::isImplementedInClass` (NativeScript/runtime/Metadata.mm) keeps a process-wide cache of `[klass alloc]`-created sample instances used to answer `respondsToSelector:` for classes whose `alloc` returns a different class or which forward messages. The cache is populated like this:\n\n1. `[klass alloc]` runs **outside** the mutex — deliberately, because `alloc` can trigger `+initialize`, which may run arbitrary code that re-enters this method (holding the lock would deadlock).\n2. The lock is then taken and `sampleInstances.emplace(klass, instance)` inserts.\n\nWhen the emplace **loses** — the re-entrant call already populated the entry for the same class, or another thread raced — the freshly allocated `instance` is abandoned: one leaked object per lost race (Instruments shows these as e.g. a leaked `UIAlertView` / `NSURLSessionConfiguration` attributed to `isImplementedInClass`). The count varies run to run since it is timing-dependent.\n\n## Why the obvious fixes don't work\n\n- **Releasing the loser** is unsafe: the instance is `alloc`'d but never `init`'d, so `-release` runs `-dealloc` against zero-filled ivars of an arbitrary framework class, on whatever thread the probe ran on (it demonstrably runs on worker threads). Benign for most classes, but a `-dealloc` that does `CFRelease`/`dispatch_release` on a zero ivar traps, and UIKit teardown off the main thread is asserting territory.\n- **Holding the lock across `alloc`** deadlocks via the `+initialize` re-entry described above.\n- **Parking losers in a static container** merely converts the unreachable leak into intentional perpetual retention — it silences Instruments without reclaiming anything (tried and reverted in #458).\n\n## Potential solutions\n\n- A re-entrancy-aware locking scheme (recursive mutex, or a reader/writer arrangement) so the populate path can be made atomic with respect to re-entrant probes without deadlocking through `+initialize`.\n- Reuse already-`alloc`-ed objects: when a sample for that Class is requested (or something else calls `[thatClass alloc]` through the runtime), hand out / consume the cached instance instead of allocating another, so a losing instance gets used rather than abandoned.\n\nThe leak site carries a comment pointing at this issue.","author":{"url":"https://github.com/edusperoni","@type":"Person","name":"edusperoni"},"datePublished":"2026-08-27T17:17:21Z","url":"https://github.com/NativeScript/ios/issues/459"}
| 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:e6c0845c-9cd9-ab49-7ea4-32cfeaf7123b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B1B2:2564F:17F1DE7:1F857C1:6B0137F3 |
| html-safe-nonce | 76a58777b12e5ff15065ea3bf64b20b4d5e82709198cbaa378fff295a215ac7b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMUIyOjI1NjRGOjE3RjFERTc6MUY4NTdDMTo2QjAxMzdGMyIsInZpc2l0b3JfaWQiOiI2ODkxNDkxODU0MzMzNTIxODAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8fed06925e1bafd12b18aa40f4c938d2df6f18b54c9f871dbdc565418779eee7 |
| hovercard-subject-tag | issue:5270245769 |
| 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/459/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eb6479d3ffc445e5c3a97a5522e0390763a6800661c15e32dd99ca0275e5a7be/NativeScript/ios/issues/459 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eb6479d3ffc445e5c3a97a5522e0390763a6800661c15e32dd99ca0275e5a7be/NativeScript/ios/issues/459 |
| og:image:alt | Problem MethodMeta::isImplementedInClass (NativeScript/runtime/Metadata.mm) keeps a process-wide cache of [klass alloc]-created sample instances used to answer respondsToSelector: for classes whose... |
| 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