OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // Based on dartbug.com/7681 | |
5 // Verify that context chains do not lead to unintended memory being held. | |
6 | |
7 library closure_cycles_test; | |
8 | |
9 import "dart:async"; | |
10 | |
11 class X { | |
12 Function onX; | |
13 X() { | |
14 Timer.run(() => onX(new Y())); | |
15 } | |
16 } | |
17 | |
18 class Y { | |
19 Function onY; | |
20 var heavyMemory; | |
21 static var count = 0; | |
22 Y() { | |
23 // Consume large amounts of memory per iteration to fail/succeed quicker. | |
24 heavyMemory = new List(10 * 1024 * 1024); | |
25 // Terminate the test if we allocated enough memory without running out. | |
26 if (count++ > 100) return; | |
27 Timer.run(() => onY()); | |
28 } | |
29 } | |
30 | |
31 void doIt() { | |
32 var x = new X(); | |
33 x.onX = (y) { | |
34 y.onY = () { | |
35 y; // Capturing y can lead to endless context chains! | |
36 doIt(); | |
37 }; | |
38 }; | |
39 } | |
40 | |
41 void main() { | |
42 doIt(); | |
43 } | |
OLD | NEW |