0
votes

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.

1
How is the memory allocated for the individual DisplayObjects? Can you add then code that allocates and inserts a new entry into DisplayObjects? When changed to a raw pointer the clear() will not delete them resulting in a memory leak. - hmjd
show us your binding code as well - Dmitry Ledentsov
I think the issue is with binding, not Lua itself and/or pointer type. - W.B.
You might consider using another binding. Point is, binding whole classes like that is quite limited. (I am not advocating, not at all! As a proof, I am not recommending any in particular). - Bartek Banachewicz
Please don't change the question in a way it makes it completely different. If you want to ask another question, ask another question. Also SO is not going to debug the code for you. - Bartek Banachewicz

1 Answers

3
votes

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.