| Author |
Message |
Antimon

Joined: Jan 18, 2005 Posts: 875 Location: Sweden
Audio files: 6
G2 patch files: 48
|
Posted: Sat Dec 08, 2007 12:40 pm Post subject:
A tricky bug to trace |
 |
|
I spent an hour trying to figure out why code like this doesn't work:
| Code: | while (true) {
<<< "Why, howdy mister!" >>>;
misterFunction();
1::second => now;
}
8 => int variable;
fun void misterFunction() {
<<< "Today, my variable is ", variable >>>;
} |
This will output something like this:
| Code: | "Why, howdy mister!" : (string)
Today, my variable is 0
"Why, howdy mister!" : (string)
Today, my variable is 0 |
The value of variable is actually random, as I tried to rearrange stuff it changed to other values, seemingly taking them from other initializations.
I guess that this is because, since execution is stuck in the main loop, the actual initialization of variable will never happen, leaving it undefined. Comparing with Java, I think variables and members that are initialized with values are handled before any code in the encapsulating class or instance is executed (static variables need to be declared before they are used, I think). I don't know if it is possible to have ChucK do this...
/Stefan _________________ You are Wendy Mackaye, first girl on the red planet. |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 5953 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sat Dec 08, 2007 1:46 pm Post subject:
|
 |
|
Definitely a bug, another one that relates to initialisation and the order in which things are written. I thought all of those would be gone by now, there used to be similar ones that would actually crash the whole VM.
Could you add it to the WiKi on the list of things to be fixed next release? If you don't like WiKi's I could do it for you as well and link here?
In general it's wise not to define variables below your main loop, classes and functions are fine but not variables on their own, I'm surprised this ran at all :¬) The mysteries of ChucK.... _________________ while(!machine.crash() ) <<<"all is well">>>; |
|
|
Back to top
|
|
 |
Antimon

Joined: Jan 18, 2005 Posts: 875 Location: Sweden
Audio files: 6
G2 patch files: 48
|
Posted: Sat Dec 08, 2007 4:59 pm Post subject:
|
 |
|
OK, I put it up on the wiki. I was shuffling code around a bit, experimenting - I'll probably keep the main loop at the bottom from now on.
Oh, wait! Should I put it up on the page for next release? I put it on the bug request/report page... I'm not completely sure that this situation can be easily detected by a compiler.
/Stefan _________________ You are Wendy Mackaye, first girl on the red planet. |
|
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 5953 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sat Dec 08, 2007 6:15 pm Post subject:
|
 |
|
I'd put it in the list for the next release, it's just plain wrong and I agree it should be a fairly simple fix, no need to debate it or whatever.
Something quite odd is going on in this area, I had a issue where manually created Shred objects had a random id as well.
Anyway I tend to go;
-define stuff and set it up.
-define function to be sporked.
-spork it.
-repeat the last two steps as needed.
-main loop.
-classes.
-utility functions.
This order feels quite natural to me and eliminates problems like yours, I keep classes and functions that low as I tend to write those to deal with things that would otherwise be annoying or monotonous and at the bottom they are out of the way (unless they turn out to have a bug...). _________________ while(!machine.crash() ) <<<"all is well">>>; |
|
|
Back to top
|
|
 |
|