Sign in to follow this  

Can't allocate script array with an exception on context

This topic is 1121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by iraxef

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

This topic is 1121 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this