# LineCallback for ExecuteString()

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

## Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
I see. Thanks for the info!

• 11
• 20
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999846
×