Question

Edit #3:

My engine is setup in the following manner:

struct Engine {
    GetEngine()....//Singleton;
    std::vector<std::unique_ptr<DisplayObject>> DisplayObjects;
};

Then I write a few lua interfaces in the following manner:

 struct LuaObject {
    DisplayObject* ControlObject;
    void Initialize() { 
         auto NewObject=make_unique<DisplayObject>();
         Engine::GetEngine().DisplayObjects.push_back(std::move(NewObject));
         ControlObject=Engine::GetEngine().DisplayObjects.back().get();
    }
    void RemoveDisplayObject() {
        //we remove all objects just for a demonstration
        Engine::GetEngine().DisplayObjects.clear();
    }
};

The game loop in Engine will loop through DisplayObject and perform various tasks. When LUA calls RemoveDisplayObject() _CrtIsValidHeapPointer occurs.

We are removing items that are outside of LUA object, is this causing the problem?

What can cause a heap error? Should I approach this differently?


Here is the Logic in the engine:

Game (Singleton)

Contains DisplayObjects (vector of DisplayObject)

Lua Bound Class:

Calls Game::Singleton CreateDisplayObject, gets pointer to last object in DisplayObjects

Has Remove() function which calls Game::Singleton RemoveDisplayObject and passes the pointer from CreateDisplayObject <---- This causes heap corruption (when calling vector.clear()). So removing items from vector is causing the heap error.

Has some extra functions which work with the DisplayObject like moving it around, no heap corruption.

Lua:

Another class to wraps the Lua bound class

Contains Remove() function which calls Lua Bound Class Remove()

At one point the engine will load an extra lua file which calls Remove() on the lua class.

Was it helpful?

Solution

From Luabridge's Readme:

Not supported:

  • Standard containers like std::shared_ptr. (sic!)

So most probably, the fact you are using unique_ptr is totally destroying it. I recommends switching to some other binding library; or even writing something simple yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top