dart-sdk/tests/language/closure/cycles_test.dart
Robert Nystrom f205141a42 Migrate language_2/closure to NNBD.
Change-Id: Ib798b9573ee8ec924f87104684934785fadc8189
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/141702
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2020-04-02 17:29:25 +00:00

44 lines
997 B
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Based on dartbug.com/7681
// Verify that context chains do not lead to unintended memory being held.
library closure_cycles_test;
import "dart:async";
class X {
late Function onX;
X() {
Timer.run(() => onX(new Y()));
}
}
class Y {
late Function onY;
var heavyMemory;
static var count = 0;
Y() {
// Consume large amounts of memory per iteration to fail/succeed quicker.
heavyMemory = new List.filled(10 * 1024 * 1024, null);
// Terminate the test if we allocated enough memory without running out.
if (count++ > 100) return;
Timer.run(() => onY());
}
}
void doIt() {
var x = new X();
x.onX = (y) {
y.onY = () {
y; // Capturing y can lead to endless context chains!
doIt();
};
};
}
void main() {
doIt();
}