如何在 package.preload 函数中析构 C++ 类

我正在使用用 SWIG 封装的 Lua 中的 C++ 类。

由于我使用的是一个 Lua_State,我希望能够在不调用 lua_close(L) 的情况下释放特定 Lua 脚本块中的变量。

我决定使用 package.preload['name'],这样在需要时可以通过使用 require 'name' 来访问其他块中的块。

我被告知,在我执行以下操作后,package.preload 函数中的变量将被释放:

package.preload['name'] = nil
package.loaded['name'] = nil

但是,即使执行了这些操作,我的定制 C++ 类似乎也没有被析构。

这是我的完整示例代码:

Main.cpp

#include "Main.h"

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_my(L);
    lua_settop(L, 0);
    luaL_dostring(L, "package.preload['test'] = function ()\n"
                         "local test = {}\n"
                         "local class = my.Class()\n"
                         "return test\n"
                     "end\n");
    luaL_dostring(L, "require 'test'");
    luaL_dostring(L, "package.preload['test'] = nil\n"
                     "package.loaded['test'] = nil\n");
}

Main.h

#include "lua.hpp"

extern "C"
{
    int luaopen_my(lua_State *L);
}
int main();

MyBindings.h

#include "Main.h"

class Class
{
public:
    Class()
    {
        std::cout << "Class Constructed" << std::endl;
    };
    ~Class()
    {
        std::cout << "Class Destructed" << std::endl;
    };
};

MyBindings.i(用于生成 MyBindings.cpp 的 SWIG 接口)中

%module my
%{
    #include "MyBindings.h"
%}

%include <stl.i>
%include <typemaps.i>

%include "MyBindings.h"

结果:

Class Constructed

为什么类析构函数没有被调用,如何正确地在 package.preload 函数中析构类和变量?

点赞
用户1930240
用户1930240

你可以通过 obj.~class() 显式地调用析构函数。

如果你在 Lua 包装器中使用外部类,我不确定它们是否能够遵循 C++ 中存在的正确的嵌套析构函数范例。

2018-07-26 13:03:14
用户1944004
用户1944004

我无法重现你的问题,但是你的代码有其他缺点,最明显的是缺少头文件保护。Main.h文件不必要,尤其是在MyBindings.h文件中更是如此,因为它没有使用它。我不知道你使用的是哪个编译器,但void main()不是有效的C ++,标准规定是 int main()

Main.cpp

#include "lua.hpp"

extern "C" int luaopen_my(lua_State *L);

int main() {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaopen_my(L);
    lua_settop(L, 0);
    luaL_dostring(L, "package.preload['test'] = function ()\n"
                         "local test = {}\n"
                         "local class = my.Class()\n"
                         "return test\n"
                     "end\n");
    luaL_dostring(L, "require 'test'");
    luaL_dostring(L, "package.preload['test'] = nil\n"
                     "package.loaded['test'] = nil\n");
    lua_close(L);
}

MyBindings.h

#pragma once
#include <iostream>

class Class
{
public:
    Class()
    {
        std::cout << "Class Constructed" << std::endl;
    };
    ~Class()
    {
        std::cout << "Class Destructed" << std::endl;
    };
};

MyBindings.i

%module my
%{
#include "MyBindings.h"
%}

%include "MyBindings.h"

示例调用:

$ swig -c++ -lua MyBindings.i
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/lua5.2 -fPIC -shared MyBindings_wrap.cxx -o my.so -llua5.2
$ clang++ -Wall -Wextra -Wpedantic -I /usr/include/lua5.2 -L . Main.cpp -l:my.so -llua5.2
$ LD_LIBRARY_PATH=. ./a.out
Class Constructed
Class Destructed

此外,你应该注意Lua是一种垃圾收集语言,即析构函数将在垃圾收集器认为有必要时运行。你可以在C中使用 lua_gc 或在Lua中使用 collectgarbage 手动运行垃圾收集器,但 我强烈建议不要手动运行垃圾收集器,因为这通常会对性能产生负面影响(即使你手动运行它以提高性能)。只有在你运行在内存非常有限的环境中并且刚刚修剪了一个表之类的情况下,才使用手动垃圾收集器会产生好处。

无论如何,我为你准备了一个Lua示例,使用了上面编译的my.so模块。

local my = require("my")
local x = my.Class()
print("Info: Deleting x")
x = nil
print("Info: Collecting garbage")
collectgarbage()
print("Info: Done :-)")
$ lua5.2 test.lua
Class Constructed
Info: Deleting x
Info: Collecting garbage
Class Destructed
Info: Done :-)
2018-07-26 23:35:08