Sign in to follow this  
B_old

LineCallback for ExecuteString()

Recommended Posts

B_old    689
Hi, I wanted to add a LineCallback as seen in one of the samples that aborts ExecuteString after a certain time. And to test it, I set up something like this:
bool ScriptEngine::init()
{
	m_engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
		
	if (!m_engine) return false;

	m_engine->SetMessageCallback(asMETHOD(ScriptEngine, messageCallback), this, asCALL_THISCALL);

	m_defaultContext = m_engine->CreateContext();

	return true;
}


void ScriptEngine::release()
{
	if (m_defaultContext) m_defaultContext->Release();

	if (m_engine) 
	{
		m_engine->Release();
		m_engine = 0;
	}
}


bool ScriptEngine::executeString(const char *script)
{
        if (!m_engine) return false;

	return m_engine->ExecuteString(0, script, &m_defaultContext) == 0;
}


I planned to add the LineCallback to the context later and just tested the above first. The script was executed, but when exiting the application Visual Leak Detector alerted me that it found a leak. This does not happen when I call ExecuteString() without the context. Any idea what I am doing wrong? I seem to be releasing the context correctly. Any idea what I am doing wrong?

Share this post


Link to post
Share on other sites
B_old    689
I see. I never used the contexts before. Is it ok to use the same context for several ExecuteString() calls?

A stupid question maybe, but if I pass the pointer to my own context, why don't you just assume I also want to use it?

Share this post


Link to post
Share on other sites
WitchLord    4678
Because you could have forgotten to initialize the pointer. Now a question for you, if you pass a pointer to a pointer to a context to a function, why didn't you suspect it would be a return value? ;)

Yes, you can use the same context for several ExecuteString calls, but the script function must finish before you reuse the context for the next ExecuteString call, otherwise it will fail.

Regards,
Andreas

Share this post


Link to post
Share on other sites
B_old    689
Heh, perfectly valid question. It seems I even misread your first answer. It seems a bit clearer now (thanks!), so I am going to proceed with adding that line callback. What I am doing is the normal way to let ExecuteString() time out, isn't it?

Share this post


Link to post
Share on other sites
WitchLord    4678
There are only 2 ways of letting contexts time out:


1. through the line callback, where the callback will be called for each script statement, letting the callback decide whether to abort the execution or not.

2. through a secondary thread, that sleeps until the time out periods ends, and when it wakes up it calls the contexts's abort method (if it's still running, of course).


Performance wise the second option is better, but if you don't want to hazzle with multiple threads the first option is probably easier.

Regards,
Andreas

Share this post


Link to post
Share on other sites

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