Double closures = wrong byref closure capture #882

fasterthanlime opened this Issue Jul 7, 2015 · 3 comments


None yet

2 participants



Gift: cover {
  msg: String
  pad: Int

please: func (f: Func) { f() }

main: func {
  g := ("One", 0)

    g = ("Two", 0)
      g = ("Three", 0)

  g msg println()

Expected behaviour

Print "Three"

Current behavior

Print "Two" and writes to a random memory location.. which is really really bad. C compiler only emits a warning (Gift* vs Gift**)


Each closure is a struct of thunk (address of the actual C function) and context (optional pointer to context struct).

When calling closures, context needs to be created (on the stack). If a variable is assigned to in a closure, it's captured "by reference", which means we pass the address to the closure instead of the value.

Currently, when creating context, variables that are captured byref are always passed with &someVariable, even if they're in a closure that has itself captured them byref. That means the inner closure gets the address of the pointer to the address of the actual variable. Hence the random memory write.


Nice, need to see if this fixes #864.


Hey @davidhesselbom! Hmm no, looks like a different issue but it shouldn't be a hard fix.

Sorry about being the worst maintainer ever in the history of open source projects btw, just doing some selfish bugfixes because our engine for Jaakan is written in ooc.


@davidhesselbom well that wasn't hard at all :) props to @zhaihj for finding the erroneous comparison.

@fasterthanlime fasterthanlime added a commit that referenced this issue Jul 8, 2015
@fasterthanlime fasterthanlime Tests for #882 fcf365d
@fasterthanlime fasterthanlime modified the milestone: 0.9.10 Jul 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment