Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
泄漏问题或';未注册类';在luabind中返回指向派生对象的指针时 我将我的应用程序的内部内容通过Lababd公开给Lua,在C++中我有一个容器>代码> SydDypTr> /Cuff> >代码>项目< /C> >是一个抽象基类。派生类包括ItemA和ItemB_C++_Smart Pointers_Luabind - Fatal编程技术网

泄漏问题或';未注册类';在luabind中返回指向派生对象的指针时 我将我的应用程序的内部内容通过Lababd公开给Lua,在C++中我有一个容器>代码> SydDypTr> /Cuff> >代码>项目< /C> >是一个抽象基类。派生类包括ItemA和ItemB

泄漏问题或';未注册类';在luabind中返回指向派生对象的指针时 我将我的应用程序的内部内容通过Lababd公开给Lua,在C++中我有一个容器>代码> SydDypTr> /Cuff> >代码>项目< /C> >是一个抽象基类。派生类包括ItemA和ItemB,c++,smart-pointers,luabind,C++,Smart Pointers,Luabind,为了向luabind公开这些内容,我使用了几个包装器类(因为我希望容器在脚本接口中具有不同的编辑机制)。我希望能够在Lua脚本中枚举容器中的项,如下所示: container=app.container for i,event in ipairs(container.items) do print(tostring(event)) end class ContainerWrapper { public: ContainerWrapper(Container& c) : co

为了向luabind公开这些内容,我使用了几个包装器类(因为我希望容器在脚本接口中具有不同的编辑机制)。我希望能够在Lua脚本中枚举容器中的项,如下所示:

container=app.container
for i,event in ipairs(container.items) do 
  print(tostring(event))
end
class ContainerWrapper {
public:
   ContainerWrapper(Container& c) : container(c) {};
   Container&  c;  // reference to the actual container
};

class ItemWrapper {
public:
  virtual ~ItemWrapper() {};
  ItemWrapper(int itemIndex_) : itemIndex(itemIndex_) {};
  int   itemIndex;  // items are addressed by index
};

class ItemAWrapper : public ItemWrapper {
public:
  ItemAWrapper(int itemIndex_) : ItemWrapper(itemIndex_) {};
};
我遇到的问题是,我可以通过返回
ItemWrappers
的原始指针来公开此功能,但这会导致内存泄漏,因为从未调用
ItemWrapper
析构函数。如果我尝试将luabind中的包装器声明为智能指针,那么当我尝试将智能指针作为lua对象返回时,会抛出一个“尝试使用未注册的类”异常

包装器的定义如下:

container=app.container
for i,event in ipairs(container.items) do 
  print(tostring(event))
end
class ContainerWrapper {
public:
   ContainerWrapper(Container& c) : container(c) {};
   Container&  c;  // reference to the actual container
};

class ItemWrapper {
public:
  virtual ~ItemWrapper() {};
  ItemWrapper(int itemIndex_) : itemIndex(itemIndex_) {};
  int   itemIndex;  // items are addressed by index
};

class ItemAWrapper : public ItemWrapper {
public:
  ItemAWrapper(int itemIndex_) : ItemWrapper(itemIndex_) {};
};
luabind注册如下:(如果我不使用智能指针)


这是设置表中值的正确方法吗?如果我传递了一个智能指针,则是赋值生成了异常,但是如果我传递了一个原始指针,那么它似乎不会在内部将其分配到智能指针中,并且对象会泄漏。执行垃圾收集也没有帮助。

Lua已经是多态的。因此,您的
getItemsAsTable
函数不需要
new
这些
ItemWrapper
对象。只要把价值观放进去就行了。如下所示:
table[i+1]=ItemAWrapper()
。除非发生了需要使用指针的事情(例如,如果更改Lua表应该反映在C++中),否则不要麻烦。只需使用一个值。

Lua已经是多态的。因此,您的
getItemsAsTable
函数不需要
new
这些
ItemWrapper
对象。只要把价值观放进去就行了。如下所示:
table[i+1]=ItemAWrapper()
。除非发生了需要使用指针的事情(例如,如果更改Lua表应该反映在C++中),否则不要麻烦。只需使用一个值。

是否存在切片的危险?如果返回实际类型,则不会切片任何内容。如果某个派生类型没有向Lua公开,那么我会说您应该向Lua公开该类型。您如何通过值将luabind注册的类型返回为
luabind::object
?啊哈:
luabind::object(L,ItemWrapper())
是否存在切片的危险?如果返回实际的类型,那你就不会切任何东西了。如果某个派生类型没有向Lua公开,那么我会说您应该向Lua公开该类型。如何通过值将luabind注册的类型返回为
luabind::object
?aha:
luabind::object(L,ItemWrapper())
luabind::object Container::getItemsAsTable(lua_State* L)
{
  luabind::object table=luabind::newtable(L);
  for (int i=0; i<items.size(); i++) {
    table[i+1]= new ItemAWrapper(); // or function to return pointer/smart pointer
  }
  return table;
 }