Temprary object lifetime - bug or feature?

Started by
4 comments, last by WitchLord 1 year, 10 months ago

In C++ temporary object, created as part of expression, will alive for the end of full-expression. In AngelScript it is not?

In my implementation I have string in utf-16, and bytestr in utf-8, some native pseudocode:

class bytestr {
    const char* data;
    int_ptr get_cstr() const {
        // return address of string data
        return (int_ptr)data;
    }
    ~bytestr() {
        free(data);
    }
};

class string {
    bytestr toUtf8()const; // return converted to utf-8 bytestr
};

I creating wrapper for sqlite3, and write next:

class sqlite {
    int_ptr db;
    ...

    int exec(const string&in query) {
        return sqlite3_exec(db, query.toUtf8().cstr);
    }
}

In C++ temp object from query.toUtf8() will alive for all call of sqlite3_exec, and pointer from cstr stay valid, but in AngelScript after calling get_cstr temp object destoyed, and sqlite3_exec got invalid, freed pointer.

Is it bug or feature in AngelScript, and I must implicitly create temp variable?

    int exec(const string&in query) {
        bytestr tmp = query.toUtf8();
        return sqlite3_exec(db, tmp.cstr);
    }
Advertisement

This could be a bug in AngelScript.

Can you give me the configuration of your angelscript interface so I can try to reproduce this? You can use the WriteConfigToFile function from the script helper add-on to produce a text file that you can share with me. It ought to be enough for me to reproduce the problem.

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

There is my engine config: https://disk.yandex.ru/d/-3t2A39zBJb3mA

Thanks for the config.

I've reproduced the case.

The problem is that once cstr is called and it returns a uint value there is nothing that tells the compiler that the bytestr returned by toUtf8 is still needed. That's why the string is destroyed, and the pointer in the uint value becomes a dangling pointer referring to freed memory,

It's not really a bug in angelscript, but I'll look into possibly changing the compiler to defer the object destruction until the end of the expression just as is done in C++.

For now you'll need to use the work around with explicitly holding a copy of the bytestr returned by toUtf8 in a local variable.

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

I've changed the compiler in rev 2780 to defer the destruction of temporary objects to the end of expressions when asEP_ALLOW_UNSAFE_REFERENCES is turned on.

Please let me know if this works properly for you. I don't have a very large set of tests with asEP_ALLOW_UNSAFE_REFERENCES turned on, so I might have unknowingly broken some other code with this change.

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

This topic is closed to new replies.

Advertisement