Advertisement

Can't allocate script array with an exception on context

Started by November 18, 2014 03:34 AM
1 comment, last by iraxef 10 years 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.

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

Advertisement

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