LoginSignup
2
2

More than 5 years have passed since last update.

binding-generatorで変換したクラスのメモリ解放をLuaのGCに任せる

Posted at

C++で作ったクラスをLuaから扱う場合、binding-generatorを使うとGlueCodeの生成が自動化できて便利ですが、何も設定しないとLuaからインスタンス化したC++のオブジェクトが解放されません。

class Hoge {
public:
    Hoge(){};
    ~Hoge(){ log("release"); };
};
local hoge = Hoge:new()
hoge = nil
collectgarbage("collect") -- releaseは表示されない

これは、cocos2dxで使われるほとんどのクラスが独自のReferenceCountでメモリ管理されており、シーンを切り替えたタイミングやretainしたものをreleaseしたタイミングでメモリが解放されるからだと思われます。

Luaからインスタンス化したクラスをLuaのGCのタイミングで解放するには、binding-generatorに渡す.iniファイルの最後の設定を変更します。

# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = yes

コメントにも書かれている通り、script_control_cppをyesにします。これでLuaのGCでメモリが解放されます。

local hoge = Hoge:new()
hoge = nil
collectgarbage("collect") -- releaseが表示される

リンク

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2