Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mysterious problem with any #79

Closed
bpunsky opened this issue Jul 17, 2017 · 4 comments
Closed

Mysterious problem with any #79

bpunsky opened this issue Jul 17, 2017 · 4 comments

Comments

@bpunsky
Copy link
Contributor

@bpunsky bpunsky commented Jul 17, 2017

Not sure what to make of this. I was having trouble with a much more complex piece of code, thinking it was a bug on my side. When I realized it was a problem with Odin, I managed to distill it down to this.

The problem only seems to occur when using any - if I use an array of strings, this works fine. The code below will crash. If you comment the return and uncomment the one above it, you will get much stranger behavior (the last value printed four times). If you don't use a function, and instead do it all inline in the main procedure, it will work fine. If you make the function inline, it still doesn't work.

main :: proc() {
    foos: [dynamic]any;

    add :: proc(foos: ^[dynamic]any, foo: string) -> ^string {
        append(foos, foo);
        //return cast(^string)foos[len(foos)-1].data;
        return nil;
    }

    add(&foos, "A B C D");
    add(&foos, "1 2 4 3");
    add(&foos, "Apple Tree Bird Bee");
    add(&foos, "Coffee Milk Juice Tea");

    for foo in foos {
        fmt.println(foo);
    }
}
@gingerBill

This comment has been minimized.

Copy link
Member

@gingerBill gingerBill commented Jul 18, 2017

This is not technically a compiler bug.

The foo in add is allocated on the stack and when converted to an any, its stack address is used for the data field. When this stack frame is not valid any more, this is causing the problem. To solve this issue, you could either change foo: string to foo: any or allocate the data needed to ensure that the lifetime of the variable exceeds that stack frame.

@gingerBill gingerBill closed this Jul 18, 2017
@bpunsky

This comment has been minimized.

Copy link
Contributor Author

@bpunsky bpunsky commented Jul 19, 2017

Is there any method to ensure that any any is dynamically allocated rather than locally? If not, is this in the plans? For now I guess I'll create a procedure that takes an any and returns a dynamically allocated any - and kudos to you and Odin for making that a trivial thing to do!

@gingerBill

This comment has been minimized.

Copy link
Member

@gingerBill gingerBill commented Jul 19, 2017

The best way to it do something like this:

append(&foos, new_clone(foo_bar)^);

That will allocate a new type and copy the original.

@bpunsky

This comment has been minimized.

Copy link
Contributor Author

@bpunsky bpunsky commented Jul 20, 2017

Works great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.