Receiver-consuming calls: an abandoned receiver leaks, a substituted one is over-released
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 12/100
- Issue 類型
- 缺陷
- 描述清晰度
- 基本清楚
- 活躍度
- 活躍
- 技術堆疊
- cpp, javascript, objective-c
- 領域
- mobile-dev
研究方向
Start with #569 and its specs to learn the retain/release contract already established for results, then read ArgConverter::MethodCallback and Interop::ReleasesCachedOwnedReturn alongside the Caches::Instances entry to see where the receiver's reference is passed or dropped. The fixture TNSSubstitutingInit is named for a substituting initializer; use it plus AddressSanitizer specs to reproduce each of the five listed leaks before changing anything. Done means exact counts hold for every caller/implementation pair listed in the tests section.
由索引模型根據 Issue 內容生成。
描述
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 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.
1. A JS initializer that returns something else leaks its receiver
When a JS-implemented initializer returns a different object, or nil, the object allocated for the call is never released:
const X = NSObject.extend({
init() { return someOtherObject; }, // or: return null
});
X.new(); // the allocated receiver keeps its alloc +1
new X(); // same
X.alloc().init(); // same
In 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.
A JS -awakeAfterUsingCoder: that returns another object leaves the caller's consumed reference behind in the same way.
With 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.
2. Consuming calls on a receiver that already existed
For 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:
- The receiver is over-released. A native initializer that substitutes its receiver releases it, and the only reference there is belongs to the
Caches::Instancesentry. The wrapper is left pointing at an object that can be freed under it. This applies to re-initializing a wrapped object, tosuper.init…()inside a JS initializer, and toProto.initX.apply(this). - A substitute that is already wrapped leaks a retain.
Interop::ReleasesCachedOwnedReturnnever 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 issuper.init…()inside a JS initializer returning a cached singleton.
Receivers allocated for the call (X.alloc().init…(), new X()) are not affected: there the alloc +1 is the reference the initializer consumes.
Suggested fix
- 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
Consumedexception: release unless the result is not owned. - 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 inMethodCallback.
Both 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.
Tests to add
- A JS initializer returning another object, and one returning null, through
X.new(),new X()andX.alloc().init(): the allocated receiver is freed exactly once. - 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). - 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.
super.init…()inside a JS initializer returning an already wrapped object: no retain is left on it.- A JS
-awakeAfterUsingCoder:returning another object: the receiver's count is balanced.
These 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.
- 主要語言
- JavaScript
- 星號
- 154
- 分支
- 46
- 平均合併
- 4 天 2 小時
- 30 天內合併 PR
- 30
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
NativeScript/ios 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 76/100
NativeScript/ios#577 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 45/100
NativeScript/ios#572 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 45/100
NativeScript/ios#528 · 1 則留言 ·
維護者通常 1 天內回覆
-
interop.FunctionReference function passed as a block, then as a function pointer, asserts可能已有人在做 @edusperoni 於 39 天前認領。 未關閉
難度 4/5 3-5 天 新手友好度 22/100
NativeScript/ios#503 ·
維護者通常 1 天內回覆
-
難度 5/5 一週以上 新手友好度 38/100
NativeScript/ios#459 · 1 則留言 ·
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
sfbrigade/pocket-gardens#60 ·
維護者通常 5 天內回覆
-
The print preview leaves writing space short by the margins of hidden headers, footers and solutions未關閉
難度 2/5 1-3 小時 新手友好度 78/100
PreTeXtBook/pretext#3329 ·
維護者通常 1 天內回覆
-
could-have
難度 1/5 1 小時以內 新手友好度 88/100
-
01 type: bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 2 天內回覆
-
accepted enhancement
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆