v2 / vlib / v / tests / fns / closure_data_with_gc_test.c.v
26 lines · 25 sloc · 379 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1fn create_closure() fn () int {
2 x := 1234
3 c := fn [x] () int {
4 println(' >> x = ${x}')
5 return x
6 }
7 return c
8}
9
10fn test_closure_data_is_kept_alive() {
11 c := create_closure()
12 assert c() == 1234
13 $if gcboehm ? {
14 C.GC_gcollect()
15 }
16 for _ in 0 .. 1000 {
17 unsafe {
18 p := malloc(8)
19 vmemset(p, 0x33, 8)
20 }
21 }
22 $if gcboehm ? {
23 C.GC_gcollect()
24 }
25 assert c() == 1234
26}
27