← Back to team overview

ooc-dev team mailing list archive

Re: Closures and stack variables

 

You are modifying d in a closure, so it must store a pointer.  (Rock should probably warn about cases like this though)

On Jan 2, 2011, at 12:33 AM, Damian <damian.pop@xxxxxxxxx> wrote:

> Hi, I've been debugging for hours, with everything making no sense because the memory suddenly was like corrupted or something like that.
> And I think now I got the problem:
> 
> In this example:
> 
> action:Func(Int)
> setAction()
> 
> for(i in 1..10){
> 	action(i)
> }
> 
> setAction:func {
> 	d:Double = 9
> 	action = func(i:Int){
> 		d = d / (i as Double)
> 		println("step " + i toString())
> 		println("d = " + d toString())
> 	}
> }
> 
> Part of the generated c code is:
> 
> void test__setAction() {
>     lang_Numbers__Double d = 9;
>     __test_closure209_ctx* __ctx210 = lang_Memory__gc_malloc(((lang_types__Class*)__test_closure209_ctx_class())->size);
>     (*(__ctx210)) = (__test_closure209_ctx) { 
>         &(d)
>     };
>     lang_types__Closure __closure211 = (lang_types__Closure) { 
>         test____test_closure209_thunk, 
>         __ctx210
>     };
>     test__action = __closure211;
> }
> 
> void test____test_closure209(lang_Numbers__Double* d, lang_Numbers__Int i) {
>     (*d) = (*d) / (((lang_Numbers__Double) (i)));
>     lang_IO__println_withStr(lang_String____OP_ADD_String_String__String(__strLit1, lang_Numbers__Int_toString(i)));
>     lang_IO__println_withStr(lang_String____OP_ADD_String_String__String(__strLit2, lang_Numbers__LDouble_toString((lang_Numbers__LDouble) (*d))));
> }
> 
> But d:Double is in the stack, and rock is saving a pointer to it in the context, am I right?
> Is this a bug or am I doing something wrong?
> Thanks!
> _______________________________________________
> Mailing list: https://launchpad.net/~ooc-dev
> Post to     : ooc-dev@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~ooc-dev
> More help   : https://help.launchpad.net/ListHelp

References