Wierd Crash

Started by
1 comment, last by Deranged 18 years, 5 months ago
This does not happen in the tutorial example, and I cannot find the difference in my code from it. Init:

bool scriptManager::onInit()
        {
            asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
            if(engine == 0)
            {
                SLog("Failed to create script engine.");
                return false;
            }

            engine->SetCommonMessageStream((asOUTPUTFUNC_t)printf, 0);

            RegisterScriptString(engine);

            //REGISTER ALL MAIN FUNCTIONS ETC HERE!!
            engine->RegisterGlobalFunction("void Log(string &in)", asFUNCTION(scripterLog), asCALL_CDECL);

            compileScript("test.as");

            ctx = engine->CreateContext();
            if(ctx == 0)
            {
                SLog("Failed to create the context.");
                engine->Release();
                return false;
            }

            DWORD timeOut;
            if(ctx->SetLineCallback(asFUNCTION(lineCallback), &timeOut, asCALL_CDECL) < 0)
            {
                SLog("Failed to set the line callback function.");
                ctx->Release();
                engine->Release();
                return false;
            }

            return true;
        }

Load File:

bool scriptManager::compileScript(std::string scriptname)
        {
            FILE *f = fopen(scriptname.c_str(), "rb");
            if(f == 0)
            {
                SLog("Failed to open the script file.");
                return false;
            }
            
            fseek(f, 0, SEEK_END);
            int len = ftell(f);
            fseek(f, 0, SEEK_SET);
            
            std::string script;
            script.resize(len);
            int c =	fread(&script[0], len, 1, f);
            fclose(f);
            
            if(c == 0)
            {
                SLog("Failed to load script file.");
                return false;
            }
            
            if(engine->AddScriptSection(0, "script", &script[0], len, 0, false) < 0)
            {
                SLog("AddScriptSection() failed");
                return false;
            }
            
            if(engine->Build(0) < 0)
            {
                SLog("Build() failed");
                return false;
            }
            
            return true;
        }

Test.as

int helloworld(int thein)
{
    return thein;
}

It crashes on if(engine->AddScriptSection(0, "script", &script[0], len, 0, false) < 0)
Advertisement
In scriptManager::onInit() you're initializing a local engine variable instead of the class' member variable. Thus in compileScript() the engine pointer is either null or pointing to random memory depending on your scriptManager constructor.

Regards,
Andreas

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

I cannot beleive I did not catch that, thanks. This is what I get for copying directly from the tutorial and only rewriting some parts!

This topic is closed to new replies.

Advertisement