Advertisement

crash when Prepare()ing

Started by October 02, 2008 10:44 AM
1 comment, last by Seppl 16 years, 1 month ago
Hi... I wonder what I might have done wrong this time but I just can't figure out why my program always crashes on Prepare(id). Getting the id of the function using GetFunctionIDByName(0, "name") works fine (it returns something positive). The function I want to Prepare() is empty for testing purpose. However... I noticed that calls to Prepare() won't crash if they are in the same code block where AngelScript has been initialized before. Any idea?

void Code::Init()
{
    engine=asCreateScriptEngine(ANGELSCRIPT_VERSION);
    if(!engine)
	{
		allegro_message("Failed to create script engine.");
		return;
	}
	engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
	asIScriptContext *context = engine->CreateContext();
	if(!context)
	{
		allegro_message("Failed to create the context.");
		engine->Release();
		return;
	}
    engine->Discard(0);
    if(engine->AddScriptSection(0, "script", script, scriptlen-1)<0)
    {
        allegro_message("AddScriptSection() failed");
        return;
    }
    if(engine->Build(0)<0)
    {
        allegro_message("Build() failed");
        return;
    }
    iClientLoop=engine->GetFunctionIDByName(0, "ClientLoop");
    if(iClientLoop==asNO_MODULE)allegro_message("The module was not found.");
	if(iClientLoop==asERROR)allegro_message("The module was not compiled successfully.");
	if(iClientLoop==asMULTIPLE_FUNCTIONS)allegro_message("Found multiple matching functions.");
	if(iClientLoop==asNO_FUNCTION)allegro_message("blah blah");
    ready=true;
    allegro_message("%i",iClientLoop);
    context->Prepare(iClientLoop);    //will not crash
    context->Execute();
}
void Code::Run()
{
    if(ready)
    {
        context->Prepare(iClientLoop); //will crash
    }
}
In Code::Init() you're declaring a local asIScriptContext variable, so the class member that Code::Run() is using has actually never been initialized (unless you're initializing it somewhere else).

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Man! Thanks... hopefully there will be a real problem with my code next time I request for help

This topic is closed to new replies.

Advertisement