オブジェクトの所有権を取得するサードパーティ ライブラリのバインディングを作成しているため、FAQに記載されているように auto_ptr を使用しようとしています。
ラップした 2 つのクラスの例を次に示します。
typedef std::auto_ptr<Panel> PanelAutoPtr;
class NewPanelCallback {
public:
NewPanelCallback(object c) { callable = c; }
PanelAutoPtr operator() (wxWindow* parent) {
object result = callable(boost::ref(parent));
return extract<PanelAutoPtr>(result);
}
private:
object callable;
};
void Factory_register_method(Factory* f,
const wxString& id,
boost::python::object callable)
{
f->registerFactoryMethod(id, NewPanelCallback(callable));
}
class_<Factory, boost::noncopyable>("Factory", no_init)
.def("get", &Factory::get, return_value_policy<reference_existing_object>());
.def("register", &Factory_register_method);
class_<Panel, std::auto_ptr<Panel>, bases<wxWindow>, boost::noncopyable)
("Panel", init<wxWindow*, int, const wxString&>()>;
私のアプリケーションでは、プラグイン開発者がウィジェットを作成するためのファクトリ メソッドとして Python 関数を登録できます。例:
class MyPanel(shell.Panel):
def __init__(self, parent, id, name):
super().__init__(parent, id, name)
def create_panel(parent):
return MyPanel(parent, -1, "Test")
shell.Factory.get().register("some_panel", create_panel)
さて、私の問題は、私のプログラムが NewPanelCallback ファンクター (C++) を呼び出すと、呼び出し演算子が戻る前にパネル オブジェクトが削除されることです! 抽出関数呼び出しが結果オブジェクトからポインターの所有権を取得しないようです。
void create_a_panel(wxFrame* frm, NewPanelCallback& cb) {
PanelAutoPtr p = cb(frm);
frm->Add(p.get());
p.release();
}
ヒントはありますか?
解決
「抽出」を使用しないことで、最終的にこれを修正しました。これは私の新しい NewPanelCallback() です:
class NewPanelItemCallback {
public:
NewPanelItemCallback(object c) { callable = c; }
PanelAutoPtr operator() (wxWindow* parent) {
return call<Shell::PanelAutoPtr>(callable.ptr(), boost::ref(parent));
}
private:
object callable;
};
なぜこれが機能し、他の方法が機能しないのか、私にはよくわかりません。それについてのコメントをいただければ幸いです。