Skip to content

Commit 561b8a6

Browse files
committed
Explain why _retain is necessary
1 parent b6602c8 commit 561b8a6

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

Sources/JavaScriptKit/JSTypedArray.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class JSTypedArray<Element>: JSObjectRef, ExpressibleByArrayLiteral where
2121

2222
public init(length: Int) {
2323
let jsObject = Element.typedArrayClass.new(length)
24+
// _retain is necessary here because the JSObjectRef we used to create the array
25+
// goes out of scope and is deinitialized when this init() returns, causing
26+
// the JS side to decrement the object's reference count. JSTypedArray will also
27+
// call _release() when deinitialized because it inherits from JSObjectRef, so this
28+
// will not leak memory.
2429
_retain(jsObject.id)
2530
super.init(id: jsObject.id)
2631
}

0 commit comments

Comments
 (0)