Inline functions sharing iterators
-
If you want to make HISE freeze put this code in your
on init
callback.one(); inline function one() { for (i = 0; i < 20; i++) { two(); } } inline function two() { for (i = 0; i < 10; i++) { Console.print(i); } }
@Christoph-Hart Are iterators supposed to be shared between inline functions? I assume it's some kind of scope issue when one function is called inside the loop of another. Took me a while track this down as the cause of a weird execution time-out I was getting.
-
@d-healey are you saying that (i) is leaking between the 2 functions?
-
@d-healey yep cant say I've tried this but I have noted ta similar situation -two iterators running at the same time (one inside a timer callback) give "unpredictable" results.
-
@d-healey Strange, as Ulrik said it seems to leak... Problem is gone as soon as you replace
i
in one of the functions... -
@Lindon There's no timer here.
The leak is caused by one inline function being called inside the other. Inline functions are syntactic sugar, it basically copies the contents of the function to where the function is invoked.
-
@d-healey This makes sense now ;)
-
@d-healey ahh, ok I see
-
@d-healey yes I know there is no timer in there... all I'm saying is that if yu use the same index variable in an iterator in a timer and one outside a timer this also "breaks" HISE.
-
This is because the
i
variable is in fact a global variable, because inline functions do not have a scope for default variables.In order to fix it, just create a local variable before using the loop:
inline function one() { local i = 0; for (i = 0; i < 20; i++) { two(); } } inline function two() { local i = 0; for (i = 0; i < 10; i++) { Console.print(i); } }