Skip to content

Commit 649e1b2

Browse files
committed
fix(runtime): rooted scan reads initialized timers' retain counts
Every NSTimer read as retain count 0, so a timer native code owned (in a collection, or scheduled on a run loop) was demoted at the next major scan and its JS properties and weak-collection identity could be collected. Only NSTimer.alloc()'s placeholder answers -retainCount without returning; initialized timers and NSTimer subclasses answer normally. The scan now skips that placeholder's class alone, taken from [NSTimer alloc] rather than by name.
1 parent ae0abeb commit 649e1b2

4 files changed

Lines changed: 61 additions & 14 deletions

File tree

‎NativeScript/runtime/WrapperLifetime.mm‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,15 @@ bool IsRootable(id obj) {
138138
return false;
139139
}
140140

141-
bool IsTimerClass(Class cls) {
142-
static Class timerClass = objc_getClass("NSTimer");
143-
for (Class c = cls; c != nil; c = class_getSuperclass(c)) {
144-
if (c == timerClass) {
145-
return true;
146-
}
147-
}
148-
return false;
149-
}
150-
151141
// The -retainCount the native class of `cls` answers with, never one JS
152142
// implements: JS subclasses are skipped, and a class whose -retainCount JS
153143
// replaced keeps the original under the swizzled-prefixed alias. Read from the
154144
// method lists of each class, so an alias inherited from a replaced ancestor
155-
// never hides a native override below it. Null for timers, as in
156-
// ObjectManager's ownership checks: the retain count of an uninitialized
157-
// NSTimer (NSTimer.alloc()) can hang.
145+
// never hides a native override below it. Null for NSTimer.alloc()'s
146+
// placeholder, whose -retainCount does not return.
158147
IMP NativeRetainCountImp(Class cls) {
159-
if (IsTimerClass(cls)) {
148+
static Class timerPlaceholder = object_getClass([NSTimer alloc]);
149+
if (cls == timerPlaceholder) {
160150
return nullptr;
161151
}
162152
SEL selector = @selector(retainCount);

‎TestFixtures/Marshalling/TNSAllocLog.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ id TNSOwnedPrimitivesObjectAt(TNSOwnedPrimitives* holder, NSUInteger index);
133133

134134
@end
135135

136+
@interface TNSUninitializedTimer : NSObject
137+
138+
// NSTimer.alloc()'s placeholder, which a JS alloc() call never creates: its
139+
// -retainCount does not return.
140+
+ (id)placeholder;
141+
142+
@end
143+
136144
// Implements NSKeyedUnarchiverDelegate: answers the object it is given, or a
137145
// new TNSAllocLog once `substitutes` is set.
138146
@interface TNSDecodeDelegate : NSObject <NSKeyedUnarchiverDelegate>

‎TestFixtures/Marshalling/TNSAllocLog.m‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ + (NSUInteger)retainCountOf:(id)object {
300300

301301
@end
302302

303+
@implementation TNSUninitializedTimer
304+
305+
+ (id)placeholder {
306+
return [NSTimer alloc];
307+
}
308+
309+
@end
310+
303311
@implementation TNSDecodeDelegate
304312

305313
- (id)unarchiver:(NSKeyedUnarchiver*)unarchiver didDecodeObject:(id)object {

‎TestRunner/app/tests/WrapperLifetimeModesTests.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,47 @@ describe("Wrapper lifetime modes", function () {
322322
});
323323
});
324324

325+
it("keeps the JS state of a natively owned timer when every wrapper is rooted", function (done) {
326+
var owner = NSMutableArray.new();
327+
var map = new WeakMap();
328+
(function () {
329+
var timer = NSTimer.timerWithTimeIntervalTargetSelectorUserInfoRepeats(1000, TNSAllocLog.new(), "description", null, false);
330+
timer.state = 5;
331+
map.set(timer, "timer");
332+
owner.addObject(timer);
333+
})();
334+
settle(2, function () {
335+
var timer = owner.objectAtIndex(0);
336+
if (rooted && scope === "all") {
337+
expect(timer.state).toBe(5);
338+
expect(map.get(timer)).toBe("timer");
339+
}
340+
timer = null;
341+
expect(deallocs()).toBe(0);
342+
owner.removeAllObjects();
343+
settle(4, function () {
344+
if (rooted && scope === "all") {
345+
expect(deallocs()).toBe(1);
346+
}
347+
done();
348+
});
349+
});
350+
});
351+
352+
it("scans a wrapper of NSTimer.alloc()'s uninitialized placeholder", function (done) {
353+
if (!(rooted && scope === "all")) {
354+
done();
355+
return;
356+
}
357+
var placeholder = TNSUninitializedTimer.placeholder();
358+
expect(placeholder.isKindOfClass(NSTimer)).toBe(true);
359+
placeholder.state = 1;
360+
settle(2, function () {
361+
expect(placeholder.state).toBe(1);
362+
done();
363+
});
364+
});
365+
325366
it("lets an explicitly released rooted wrapper go", function (done) {
326367
var owner = NSMutableArray.new();
327368
var obj = Extended.new();

0 commit comments

Comments
 (0)