# ExecuteString

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

## Recommended Posts

I've been experiencing a problem with ExecuteString crashing my game in randomly wierd ways. The only thing I can see at the moment is that each time ExecuteString is called, it is being done while another context is executing. Is this a problem? Joe

##### Share on other sites
On second thought, it shouldnt matter at all, considering that you can ExecuteStep the ExecuteString context. It might be a really simple error on my part, but the code is pretty simple; it just calls ExecuteString... uhg.

##### Share on other sites
Well, I use ExecuteString() all the time in the test framework, so I know that the method itself is not the cause of the crashes.

You are saying that ExecuteString is run while another context is executing. Do you mean that you are using multithreading, or do you suspend the other context and run ExecuteString()?

If you are executing script contexts in multiple threads then you are most likely going to have trouble, even if the contexts belong to different engines. AngelScript is not yet supporting multithreading.

I'll do some tests with concurrent script execution to see if that is the problem. But without more info, I think it will be very difficult to find the exact problem you're having.

##### Share on other sites
I'm still stepping through the code to figure out what is going on, but the engine is single threaded. The game works by invoking scripts based on the user's input, e.g. "void cmd_execute(string buffer)." If the user typed "exec this->SetWhatever(1)," then the engine would magically call the cmd_execute method with buffer set to "this->SetWhatever(1)." cmd_execute would then invoke a engine function that would in turn call ExecuteString.

The original call to cmd_execute does not actually finish until after it invokes ExecuteString.

(The "this" pointer is a pointer to the game object invoking the script.)

##### Share on other sites
Is cmd_execute() a script function?

That would make it a situation where a context causes another context to be executed. Theoretically this should work, though I don't have a test case for it (I do believe Lennart Denninger's BeatHarness have some situations like this though.) I'll write some test cases to verify this functionality.

##### Share on other sites
Yes, cmd_execute() is a script function. I guess I worded the problem poorly. My concern was that ExecuteString might be messing up AngelScript's state, such that when it is finishes, the original context (e.g. cmd_execute) is unstable.

##### Share on other sites
It could be that this is what is happening, and if that is so it is because of a bug in AngelScript.

I hope I'll get the time to test this further tomorrow.

Hmm, come to think of it. ExecuteString() can't call a function that in turn calls ExecuteString() as the context is shared for all calls to ExecuteString(). Maybe I can fix this by pushing the context on a stack. I don't know if this is your scenario, but it is something that I have to fix, or at least prevent.

##### Share on other sites
I ran the following test:

#include "utils.h"#include "stdstr.h"using namespace std;#define TESTNAME "TestNested"static const char *script1 ="void TestNested()                         \n""{                                         \n""  CallExecuteString(\"i = 2\");           \n""  i = i + 2;                              \n""}                                         \n";static void CallExecuteString(string &str){	asIScriptContext *ctx = asGetActiveContext();	asIScriptEngine *engine = ctx->GetEngine();	engine->ExecuteString(0, str.c_str());}static int i = 0;bool TestNested(){	bool fail = false;	asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);	RegisterStdString(engine);	engine->RegisterGlobalProperty("int i", &i);	engine->RegisterGlobalFunction("void CallExecuteString(string &)", asFUNCTION(CallExecuteString), asCALL_CDECL);	COutStream out;		engine->AddScriptSection(0, TESTNAME, script1, strlen(script1), 0);	engine->Build(0, &out);	// Make the call with a separate context (should work)	asIScriptContext *ctx;	engine->CreateContext(&ctx);	ctx->Prepare(engine->GetFunctionIDByIndex(0, 0));	ctx->Execute();	if( i != 4 )	{		printf("%s: Failed to call nested ExecuteString() from other context\n", TESTNAME);		fail = true;	}	ctx->Release();	// Make the call with ExecuteString (doesn't work)	i = 0;	engine->ExecuteString(0, "TestNested()");	if( i != 4 )	{		printf("%s: Failed to call nested ExecuteString() from ExecuteString()\n", TESTNAME);		fail = true;	}	engine->Release();	// Success	return fail;}

As I thought the first call with a proper context works as expected, where as the second call through ExecuteString() fails silently, with the result that i == 2.

I'll fix this so that ExecuteString() can be nested by pushing the current context on the stack. That is if it is possible to do that, otherwise I'll just make ExecuteString() fail if it is already active. In that case CallExecuteString() ought to raise an exception for the script.

NOTE. The code above is using the latest WIP with default values for some of the parameters.

##### Share on other sites
Figured out my problem. I was passing a string from the engine to a script by value and the string object's destructor was getting called twice.