ELuna 注册 Lua 绑定

使用的 https://github.com/radiotail/eluna 提供的头文件,可注册类和函数,这个头文件提供了一个垃圾收集器,用来把绑定过的对象释放掉,那个循环释放写的有点问题。

原始

struct CPPGarbage

{

inline static void pushMethod(GenericMethod* method) { m_CPPMethods.push_back(method);};

inline static void pushFunction(GenericFunction* function) { m_CPPFunctions.push_back(function);};

inline static void release() {

for (Function_Vector::iterator itr = m_CPPFunctions.begin(); itr != m_CPPFunctions.end(); ++itr) {

delete *itr;

}

for (Method_Vector::iterator itr = m_CPPMethods.begin(); itr != m_CPPMethods.end(); ++itr) {

delete *itr;

}

}

private:

static Function_Vector m_CPPFunctions;

static Method_Vector m_CPPMethods;

};

Function_Vector CPPGarbage::m_CPPFunctions;

Method_Vector CPPGarbage::m_CPPMethods;

改为

struct CPPGarbage
    {
        inline static void pushMethod(GenericMethod* method) { m_CPPMethods.push_back(method);};
        inline static void pushFunction(GenericFunction* function) { m_CPPFunctions.push_back(function);};

        inline static void release() {
            for (Function_Vector::iterator itr = m_CPPFunctions.begin(); itr != m_CPPFunctions.end(); ++itr) {
                if (*itr) {
                    delete *itr;
                    *itr = NULL;
                }
            }

            for (Method_Vector::iterator itr = m_CPPMethods.begin(); itr != m_CPPMethods.end(); ++itr) {
                if (*itr) {
                    delete *itr;
                    *itr = NULL;
                }
            }
        }
    private:
        static Function_Vector m_CPPFunctions;
        static Method_Vector   m_CPPMethods;
    };
    //Function_Vector CPPGarbage::m_CPPFunctions;
    //Method_Vector   CPPGarbage::m_CPPMethods;
    __declspec(selectany) Function_Vector CPPGarbage::m_CPPFunctions;

    __declspec(selectany) Method_Vector   CPPGarbage::m_CPPMethods;

你可能感兴趣的:(ELuna 注册 Lua 绑定)