AngelScript Crash - Object Handle Array

Post Reply
User avatar
Johannes
Posts: 1374
Joined: Thu Dec 18, 2008 1:26 am
Contact:

AngelScript Crash - Object Handle Array

Post by Johannes » Sun Dec 11, 2011 9:26 pm

I have some AngelScript code that compiles and runs fine the first time I start a level, but when I save the file again, which causes the editor to re-compile the code, it crashes the engine completely.


EDIT 1
Here is a condensed bit of code that illustrates the crash. To test it open Data/Scripts/level.as and replace this code at the beginning:

Code: Select all

void Init(){
}
with this code:

Code: Select all

class obj{}
obj@[] obj_handle_array(2); // this line causes the crash on re-compile.
void Init(){
    obj temp_obj = obj();
}
EDIT 2
You can avoid the crash when you move marked line into the init() function. For example this compiles fine every time:

Code: Select all

class obj{}
void Init(){
    obj@[] obj_handle_array(2); // no longer causes issues
    obj temp_obj = obj();
}
EDIT 3
Of-course this way obj_handle_array will only exist in the scope of init(). If you want to have access to the array outside of init(), as well as avoid the Crash, you can do this:

Code: Select all

class obj{}
obj@[] obj_handle_array; // declares object handle array.
void Init(){
    obj@[] obj_handle_array(2); // initializes object handle array.
    obj temp_obj = obj();
}

The code in the original post tries to illustrate the need for such an odd array declaration, and was mostly intended to illustrate that the code was working, just not re-compiling.

ORIGINAL POST:

Basically I'm using Arrays, and object handles:

Code: Select all

/* Defining Variables and Objects*/
//An array of object handles of length 2.
my_object@[] my_array(2); // This specifically seems to be what prevents the code from re-compiling.

my_object test_object_one;
my_object test_object_two;


/* Defining Object Classes */
class my_object{
    int my_value;
    // object constructor
    my_object(int new_value){
        this.my_value = new_value;
    }
}

/* Defining Functions Classes */
// this function is passed a handle to an object so the object can be modified directly.
void mod_object(my_object@ object_handle){
    object_handle.my_value*=2;
}

// this is called when a level is first started.
void Init(){
    test_object_one = my_object(1);
    test_object_two = my_object(10);

    @my_array[0] = @test_object_one;
    @my_array[1] = @test_object_two;

    Print("object one value:"+my_array[0].my_value+"\n");//should print 1
    Print("object two value:"+my_array[1].my_value+"\n");//should print 10

    mod_object(@my_array[0]); // function modifies the object that the handles in the array reference
    mod_object(@my_array[1]);

    Print("object one value:"+my_array[0].my_value+"\n");//should print 2
    Print("object two value:"+my_array[1].my_value+"\n");//should print 20
}
This code creates an array of object handles. Object handles allow you to pass them as arguments to a function, and then have the function modify the original object that they refer to, or are the 'handle' of, similar to what you can do with pointers.

I then just use a function that takes an object handle as an argument and modifies the original object to test that the handles are working correctly and then write some stuff to the console/terminal that shows those changes:
Image



You can try this by opening Data/Scripts/level.as and replacing the "void Init(){}" line with the code I listed above.

Then start the alpha, open a level and look at the console (for mac you'll need to start it from the terminal, windows should always have it). It should output the values correctly as I mentioned in my comments, showing that everything works, but when you open level.as, and save it again, the alpha should crash.

The console/terminal will say "Bus error" and stop, and give a crash dump: http://pastebin.com/L5yP6XGy


I think you can recreate this error by just creating a custom object handle array, like so:

Code: Select all

my_object@[] my_array(2);
This will initialize an array of object handles for 'my_object' objects, with the name of 'my_array' and the length of 2. (for this to work you need to first define the 'my_object' class, as you can see in my code at the top). for some reason this will cause a crash when the editor tries to re-compile it.


I'd like to submit this as a bug report to david, but would like more people to test it first, or correct me if I did something wrong.

User avatar
Silverfish
Posts: 1451
Joined: Sun Oct 26, 2008 8:24 pm
Location: Sweden
Contact:

Re: AngelScript Crash - Object Handle Array

Post by Silverfish » Sun Dec 11, 2011 9:49 pm

I tested it for you (as you know) and I got the same result, sans the console output.

User avatar
Johannes
Posts: 1374
Joined: Thu Dec 18, 2008 1:26 am
Contact:

Re: AngelScript Crash - Object Handle Array

Post by Johannes » Mon Dec 12, 2011 4:07 am

I have narrowed down the crash to a very specific bit of code. I had suspected this before, but hadn't fully tested it yet. OP edited accordingly.

User avatar
Johannes
Posts: 1374
Joined: Thu Dec 18, 2008 1:26 am
Contact:

Re: AngelScript Crash - Object Handle Array

Post by Johannes » Mon Dec 12, 2011 4:34 am

And I've figured out a work-around that helps you avoid the crash all-together.

Post Reply