Skip to content

Commit acdfe8d

Browse files
committed
fix(runtime): keep waiting releases adoptable while their batch drains
The drain took the whole batch out of the queue before releasing anything. A release that ran JS (a -dealloc implemented in JS) could wrap an object whose release was later in the batch: the takeover missed it, the wrapper retained on top of the waiting release, and neither the retain nor the later release crossed the count at which the swizzle protects, so a natively held instance lost its wrapper at the next collection. One release leaves the queue at a time now.
1 parent 85fa00f commit acdfe8d

2 files changed

Lines changed: 83 additions & 22 deletions

File tree

‎NativeScript/runtime/ObjectManager.mm‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,23 @@ void UnlinkRegistered(ObjectWeakCallbackState* state) {
122122
bool drainPosted = false;
123123
};
124124

125+
// A release can run JS (a -dealloc implemented in JS) that wraps an object
126+
// whose own release still waits, or that reaches this drain again. So one
127+
// release leaves the queue at a time, the rest stay there for
128+
// TakeDeferredRelease, and nothing is held across a release.
125129
void DrainDeferredReleases(DeferredReleases* pending) {
126-
std::unordered_map<void*, unsigned> objects;
127-
std::vector<const void*> blocks;
128-
objects.swap(pending->objects);
129-
blocks.swap(pending->blocks);
130130
pending->drainPosted = false;
131-
for (const auto& entry : objects) {
132-
id obj = (__bridge id)entry.first;
133-
for (unsigned i = 0; i < entry.second; i++) {
134-
[obj release];
131+
while (!pending->objects.empty()) {
132+
auto it = pending->objects.begin();
133+
id obj = (__bridge id)it->first;
134+
if (--it->second == 0) {
135+
pending->objects.erase(it);
135136
}
137+
[obj release];
136138
}
137-
for (const void* block : blocks) {
139+
while (!pending->blocks.empty()) {
140+
const void* block = pending->blocks.back();
141+
pending->blocks.pop_back();
138142
Block_release(block);
139143
}
140144
}

‎TestRunner/app/tests/GCFinalizerTests.js‎

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ describe("GC finalizer callbacks", function () {
118118
})(TNSApi);
119119
}
120120

121+
function collectAndWait(rounds, callback) {
122+
scrubStack();
123+
__collect();
124+
__collect();
125+
setTimeout(function () {
126+
if (rounds > 1) {
127+
collectAndWait(rounds - 1, callback);
128+
} else {
129+
callback();
130+
}
131+
}, 10);
132+
}
133+
121134
[
122135
{
123136
suffix: " (extend)",
@@ -170,19 +183,6 @@ describe("GC finalizer callbacks", function () {
170183
retainCount = again.retainCount();
171184
});
172185

173-
function collectAndWait(rounds, callback) {
174-
scrubStack();
175-
__collect();
176-
__collect();
177-
setTimeout(function () {
178-
if (rounds > 1) {
179-
collectAndWait(rounds - 1, callback);
180-
} else {
181-
callback();
182-
}
183-
}, 10);
184-
}
185-
186186
var attempts = 200;
187187
(function poll() {
188188
if (retainCount === -1 && --attempts > 0) {
@@ -208,6 +208,63 @@ describe("GC finalizer callbacks", function () {
208208
});
209209
});
210210

211+
// Releases wait in batches, and one of them can run JS (a -dealloc
212+
// implemented in JS) that wraps an object whose own release comes later
213+
// in the same batch. The order within a batch is unspecified, so the
214+
// instance whose -dealloc does the wrapping is rarely the last one.
215+
it("lets a wrapper adopt a release that waits behind the one running JS", function (done) {
216+
var Held = defineTakeoverWithExtends(function () {});
217+
var count = 16;
218+
var holder = NSMutableArray.alloc().init();
219+
var held = [];
220+
for (var i = 0; i < count; i++) {
221+
var instance = Held.alloc().init();
222+
held.push(instance);
223+
holder.addObject(instance);
224+
}
225+
226+
var wrapped = 0;
227+
var Trigger = TNSApi.extend({
228+
methodCalledInDealloc: function () {
229+
for (var j = 0; j < count; j++) {
230+
holder.objectAtIndex(j).marker = j;
231+
wrapped++;
232+
}
233+
}
234+
}, { name: "TNSApiReleaseBatchTrigger" });
235+
var trigger = Trigger.alloc().init();
236+
237+
var queue = dispatch_get_global_queue(qos_class_t.QOS_CLASS_DEFAULT, 0);
238+
dispatch_async(queue, function () {
239+
for (var k = 0; k < count; k++) {
240+
__releaseNativeCounterpart(held[k]);
241+
}
242+
held = null;
243+
__releaseNativeCounterpart(trigger);
244+
trigger = null;
245+
});
246+
247+
var attempts = 200;
248+
(function poll() {
249+
if (wrapped === 0 && --attempts > 0) {
250+
setTimeout(poll, 10);
251+
return;
252+
}
253+
expect(wrapped).toBe(count);
254+
collectAndWait(2, function () {
255+
var markers = [];
256+
var expected = [];
257+
for (var m = 0; m < count; m++) {
258+
markers.push(holder.objectAtIndex(m).marker);
259+
expected.push(m);
260+
}
261+
expect(markers).toEqual(expected);
262+
holder.removeAllObjects();
263+
done();
264+
});
265+
})();
266+
});
267+
211268
// A GcProtected (natively retained) instance is resurrected by its
212269
// finalizer, but anything reachable only through it is queued in the same
213270
// GC and disposed — the resurrected parent then holds "husks" whose

0 commit comments

Comments
 (0)