Advertisement

Obscure bug: Initial-variable binding can be "tricked" by calling from function: 'Null pointer access'.

Started by December 03, 2020 09:11 PM
2 comments, last by Zero_One_01 3 years, 11 months ago

Consider the following code:

class int_holder{
int value;
int_holder(int x){value=x;}}

int_holder sink(2);

int test_1(int_holder @a = sink){a.value++;return a.value;}
int test_2(){return test_1();}

int b = test_2();

This fails with the error:

ANGELSCRIPT: ERR  : Failed to initialize global variable 'b'
ANGELSCRIPT: INFO : Exception 'Null pointer access' in 'int test_2()

I suspect the code fails because the initialization for b is run before sink is initialized. I can make it work as intended with either int b = test1() or int b = int_holder(test_2()).value;. This seems to run test_2() after sink was initialized, thus no bug occurs.

Default values can't combine with delayed function calls unless the initialization would be more lenient somehow (a lazy suggestion would be that if Null pointer access errors "would occur" run another initialization pass and see if any of them are resolved. If none of them are resolved, then fail with said error.)

Thanks for raising this. I'll investigate on what can be done to resolve this.

Running multiple initialization passes would be complicated, because it is possible that the first pass did something before the error occured, e.g. open a file handle, or committing some data to a database, etc.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

I meant on subsequent passes attempt to re-initialize only those variables that failed on the previous initialization.

For example, here b1 throws an error because it attempts to read from int_holder sink which has yet to be initialized in the first pass. So, once the first pass finishes, b1 will be the only variable that has failed. Hence run a second pass trying to re-initialize only those variables that failed in the first one (in our case, only b1 ) . Repeat until either all failed variables are initialized or a pass has failed to initialize any new variables.

That was just speculative, I'm not familiar with how Angelscript's initialization algorithm actually works. Thank you for your time and efforts in developing this.

This topic is closed to new replies.

Advertisement