🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Can't allocate script array with an exception on context

Started by
1 comment, last by iraxef 9 years, 7 months ago

I'm seeing an assert trigger in the following situation:


CScriptArray* MyFunction(const std::string& name, MyAppType* foo)
{
    asIObjectType* objectType = ...;

    if ( name.empty() )
    {
        asGetActiveContext()->SetException("Please provide a non-empty name");

        CScriptArray* scriptArray = CScriptArray::Create(objectType);
        assert( NULL != scriptArray ); // this asserts

        return scriptArray;
    }

    ...
}

What I'd prefer is that the function returns, but then I see my script exception (via whatever means I'm using to catch/display them).

If I allocate the array first, and then set the exception, there is no assert:


CScriptArray* MyFunction(const std::string& name, MyAppType* foo)
{
    asIObjectType* objectType = ...;

    if ( name.empty() )
    {
        CScriptArray* scriptArray = CScriptArray::Create(objectType);
        assert( NULL != scriptArray ); // this does not assert

        asGetActiveContext()->SetException("Please provide a non-empty name");

        return scriptArray;
    }

    ...
}

I can easily work around this (perhaps I should be returning NULL anyway since I've already set the script exception..) but FYI.

Advertisement

If I understand what you're trying to do, you shouldn't create the array since you already detected an error condition. Instead you should just return null.

On the other hand, the creation of the array shouldn't fail just because you've set an exception on the current context. I'll need to make some adjustments in the CScriptArray add-on to handle this scenario better.

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 see what you mean:


	// It's possible the constructor raised a script exception, in which case we
	// need to free the memory and return null instead, else we get a memory leak.
	if( ctx && ctx->GetState() == asEXECUTION_EXCEPTION )
	{
		a->Release();
		return 0;
	}

	return a;

I guess you could check whether the context had an exception before the ctor was called... but then what if the ctor added another exception? Do you have any way to check whether there are now 2 exceptions?

This topic is closed to new replies.

Advertisement