Sign in to follow this  
RCL

Problem (bug?) when releasing suspended context

Recommended Posts

Hi! Is there a way to Abort() or Release() context (suspended one) without resuming its execution (what engine seemingly does)? The situation is: I have script which runs in almost-infinite :) loop and is executed via ExecuteStep(). So its context remains suspended between polls. When the program quits, context->Release() is called and AngelScript crashes when releasing BSTR instance. Calling context->Abort() prior to Release() does the same. I investigated the problem and found out that context tries to execute some cleanup code on exit. I'm not sure that it is safe - engine is already being shut down and if that code calls any external functions (like BSTR ones) they may crash. So, is there a (safe) way to release the context at once, without any bytecode execution? (However, I feel it is not correct - at least with current AngelScript). If there's no, I think I'll hack the code (for now) just to keep it from crashing :) Cheers, RCL

Share this post


Link to post
Share on other sites
This is probably a bug in AngelScript. The engine should be available to the contexts even after it has been released by the host application.

I'll have to investigate the situation further before I can give a definite answer. From what you describe, you're doing it correctly.

Share this post


Link to post
Share on other sites
I'm trying to verify this bug, but I've yet to reproduce it. I ran the following test case with version 1.9.1WIP2:


//
// Tests releasing a suspended context
//
// Test author: Andreas Jonsson
//

#include "utils.h"
#include "stdstr.h"

namespace TestSuspend
{

#define TESTNAME "TestSuspend"


static int loopCount = 0;

static const char *script1 =
"string g_str = \"test\"; \n" // variable that must be released when module is released
"void TestSuspend() \n"
"{ \n"
" string str = \"hello\"; \n" // variable that must be released before exiting the function
" while( true ) \n" // never ending loop
" { \n"
" loopCount++; \n"
" } \n"
"} \n";

bool Test()
{
bool fail = false;

asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

RegisterStdString(engine);

engine->RegisterGlobalProperty("int loopCount", &loopCount);

COutStream out;
engine->AddScriptSection(0, TESTNAME ":1", script1, strlen(script1), 0);

engine->Build(0, &out);

asIScriptContext *ctx;
engine->CreateContext(&ctx);
ctx->Prepare(engine->GetFunctionIDByDecl(0, "void TestSuspend()"));

while( loopCount < 5 )
ctx->ExecuteStep(0);

// Release the engine first
engine->Release();

// Now release the context
ctx->Release();

// Success
return fail;
}

} // namespace





When engine->Release() is called nothing happens since the context is still holding a reference to the engine. When ctx->Release() is called it will call the CleanStack bytecode to free the local string variable. Then it will release the engine, which results in @exit() to be called for releasing the global string variable. All this happens without trouble.

Even though I couldn't reproduce the bug in the currently released version I still thank you, because I was able to detect and fix a bug in the version I'm about to release.

Let me know if you're doing something differently than I do in the test case.

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
I'm trying to verify this bug, but I've yet to reproduce it. I ran the following test case with version 1.9.1WIP2:


That test works fine for me, too...

Quote:
Original post by WitchLord
Let me know if you're doing something differently than I do in the test case.


The difference is that my loop can exit at any time, it does not depend on any script variable like yours one. Perhaps it also means that it can exit during calls etc.

Cheers,
RCL

Share this post


Link to post
Share on other sites
I see that this is going to be tough to find this bug. But with your help, I'm sure I will be able to.

ExecuteNext() only allow the suspension of the execution at certain bytecodes, namely: CALL, CALLSYS, CALLBND, RET, JMP, JZ, JNZ, JS, JNS, JP, JNP, JMPP.

Is it possible for you to check the last byte code executed before releasing the context?

Better yet, is it possible for you to show me the script that crashes?

Are you calling any application registered functions from the script that call context->Suspend()?

You said in your original post that context is trying to execute the cleanup code after the engine is shut down. In my tests this cannot happen, since the context holds a reference to the engine. How did you come to this knowledge?

I'm suspecting that somehow a bstr is being destroyed more than once. But I need your help to confirm this.

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
I see that this is going to be tough to find this bug. But with your help, I'm sure I will be able to.

[... skipped by RCL ...]



I'm terribly sorry, but right now I am on my vacations and can't check that. I'll be back in the middle of October :)

Cheers,
RCL

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