Advertisement

Segmentation fault when trying to prepare an object's context while there is multiple objects in a vector array

Started by August 23, 2022 05:35 AM
4 comments, last by Varrok 2 years, 3 months ago

i have an entity class with angelscript engines and contexts as member variables. however when this class is apart of a vector array with more than one entity it crashes with a seg fault at the context preparing before executing,
Example code:

class test{

private:

asIScriptEngine *engine = asCreateScriptEngine();

asIScriptModule *ScriptModule;

asIScriptFunction *AngelUpdate;

asIScriptContext *ctx;

CScriptBuilder builder;

public:

test(){

builder.StartNewModule(engine, "MyModule");

builder.AddSectionFromFile("test.as");

builder.BuildModule();

ScriptModule = engine->GetModule("MyModule");

AngelUpdate = ScriptModule->GetFunctionByDecl("void main()");

ctx = engine->CreateContext();

ctx->Prepare(AngelUpdate);

ctx->Execute();

}

~test(){

ctx->Release();

engine->ShutDownAndRelease();

}

void update(){

ctx->Prepare(AngelUpdate);

ctx->Execute();

}

};


int main()

{

std::vector<test> entityvector{};

entityvector.emplace_back();

//one more emplace and there will be a crash.

while(true)

{

entityvector[0].update();

}

return 0;

}

strangely having multiple objects separate from a vector does not cause this crash.

this is kinda demotivating and im thinking of just moving to godot at this point xd


Doesn't seem like Angelscript issue

https://stackoverflow.com/questions/21646324/why-is-emplace-back-calling-destructor

Check the destructor code, it gets called at the second emplace_back(), it shuts down the engine. Before that it copies the pointers, but they quickly become dead due to originals being released.

Advertisement

I can`t say I`m an expert in containers however vector and array are two different type of containers. Array is also a raw list/collection of items in c# and c++

objects in a vector array

that should be objects in a vector container.

My project`s facebook page is “DreamLand Page”

@Varrok thank you this helps very much. although i havent implemented a solution, im not that advanced at c++ but ill try to figure this out :p

anothaxboii said:

@Varrok thank you this helps very much. although i havent implemented a solution, im not that advanced at c++ but ill try to figure this out :p

For example, you can only release context + engine shutdown in a manually called function (with destructor not doing that), or implement a copy constructor that re-inits all the pointers by doing what the constructor did, or, I think it'd be possible though a little tricky, to wrap the angelcode stuff in a shared pointer object that only calls the release + shutdown when it (the smart pointer) is no longer used

This topic is closed to new replies.

Advertisement