删除我在 C++ 中的所有 Lua 用户数据

发布于 2025-01-03 05:28:45 字数 657 浏览 1 评论 0原文

我想知道是否可以访问所有用户数据“表”(它称为用户数据表吗?),然后从 Lua 中删除它们,因为这是我的问题:

a = Object(5, 5)
a:Delete()
a:SetPosition(3,3)

正如你首先看到的,我创建了一个对象并保存了一个指向一个名为 Object 的 C++ 类,它是在我的地图类中使用“new”分配的。然后我删除该对象,这意味着我删除了映射类中为指针分配的内存。最后我调用 SetPosition,如果内存仍然分配给 C++ Object 类,那么一切都会运行得很有趣。但如果它是删除(在本例中是因为我们在 SetPosition(...) 调用之前调用了 Delete()),我的程序将崩溃。所以我想知道的是:

是否可以通过调用Delete将lua中的变量'a'设置为nil?我知道如果删除返回 nil 我可以做类似 'a = a:Delete()' 的事情,但如果我忘记做 'a =' 部分它就会失败。另外我想知道是否可以删除用户数据并在调用 SetPositon() 时检查它是否不存在,如果不存在我将返回。

另外,基本代码来自: http://lua-users.org/wiki/SimpleCppBinding

I'm wondering if it's possible to access all of the userdata "tables" (is it called userdata tables?) and then delete them from Lua because this is my problem:

a = Object(5, 5)
a:Delete()
a:SetPosition(3,3)

As you can see first I create an object and save an pointer to a c++ class called Object which is allocated using "new" in my map class. Then I delete the object which means I delete the allocated memory for the pointer in my map class. And last I call SetPosition, if the memory still is allocated for the c++ Object class everything will run fun. But if it is deletes (as it is in this case because we called Delete() before the SetPosition(...) call) my program will crash. So what I'm wondering is following:

Is it possible to set the varaible 'a' in lua to nil by calling Delete ? I know I could do something like 'a = a:Delete()' if Delete return nil but if I forget to do the 'a =' part it fail. Also I'm wondering if it's possible to delete the userdata and check if it doesn't exist when I call SetPositon(), if it doesn't I will just return.

Also, the base code is from: http://lua-users.org/wiki/SimpleCppBinding

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

禾厶谷欠 2025-01-10 05:28:45

首先,我来回答你的问题:

是否可以通过调用Delete将lua中的变量'a'设置为nil?

不,没有办法按照你所说的去做。这是有原因的:你正在尝试做的是糟糕的代码。

Lua 是一个垃圾收集系统。 Lua 不应该删除对象。如果 Lua 得到一个指向某个外部对象的指针,那么要么你的代码拥有它,要么 Lua 拥有它。

如果你的代码拥有它,Lua 不应该删除它。 Lua 可以使用一段时间。但这取决于您的 Lua 代码是否准确且仅在该时间段内使用它。一旦它的生命周期结束,Lua 就不应该再和它对话了。

这与处理 C 和 C++ 中的对象指针没有什么不同。如果您的函数被赋予裸指针(即:不是智能指针),您的代码需要知道它可以合理预期与该对象对话多长时间。可以储存吗?指向该对象的指针可以存储多长时间?该物体什么时候会死亡,谁负责摧毁它?

如果您将一个对象传递给 Lua,使得 Lua 现在拥有该对象,那么 Lua 也不应该显式删除它。 Lua是一个垃圾收集系统;你应该在你的类型上附加一个__gc元方法,这样Lua的垃圾收集器就会在收集用户数据时调用你的代码。这样,你就可以调用析构函数、释放内存等。

当你给 Lua 一些现在属于 Lua 的东西时,它应该看起来像一个常规的 Lua 对象。您不会为在 Lua 中创建的表和字符串调用 Delete 方法;你让垃圾收集器完成它的工作。作为编写 C++ 到 Lua 接口的人,您的工作就是确保您提供给 Lua 的对象按照 Lua 想要的方式运行。

如果您需要进行大量的资源管理,您希望 Lua 尽快释放资源(例如文件句柄等),那么您需要在内部存储一个指向 C++ 对象的指针 > 非轻用户数据。这就是您 NULL 输出的指针。该对象上的所有接口函数都会检查指针以查看它是否为 NULL,并且不执行任何操作或引发错误。

Lua 的文件句柄(由 io.open 返回)就是一个很好的例子。如果你尝试调用它们的函数,Lua 会抛出一个 Lua 错误。

First, let me answer your question:

Is it possible to set the varaible 'a' in lua to nil by calling Delete ?

No. There is no means to do what you're saying. And there's a reason for that: what you're trying to do is terrible code.

Lua is a garbage collected system. Lua should not be expected to delete objects. If Lua gets a pointer to some external object, then either your code owns it or Lua owns it.

If your code owns it, Lua should not be deleting it. Lua can use it for some period of time. But it is up to your Lua code to use it for exactly and only that period of time. Once its lifetime has expired, Lua shouldn't be talking to it anymore.

This is no different from dealing with pointers to objects in C and C++. If your function is handed a naked pointer (ie: not a smart pointer), your code needs to know how long it can reasonably expect to talk to that object. Can it store it? How long can it store a pointer to that object? When will that object die, and who's responsible for destroying it?

If you pass an object to Lua such that Lua now owns the object, Lua shouldn't be explicitly deleting it either. Lua is a garbage collected system; you should attach a __gc metamethod to your type, so that Lua's garbage collector will call your code when the userdata is collected. That way, you can call destructors, free memory, etc.

When you give Lua something that now belongs to Lua, it should look like a regular Lua object. You don't call Delete methods for tables and strings you create in Lua; you let the garbage collector do its job. It is your job, as the one writing the C++-to-Lua interface, to ensure that the objects you give to Lua behave the way that Lua wants them to.

In cases where you need to do significant resource management, where you want Lua to release resources as quickly as possible (such as for file handles, etc), then you need to store a pointer to your C++ object inside of the non-light userdata. That's the pointer you NULL out. All of your interface functions on that object will check the pointer to see if it's NULL and simply do nothing or raise an error.

Lua's file handles (returned by io.open) are a good example of this. If you try to call functions on them, Lua throws a Lua error.

那一片橙海, 2025-01-10 05:28:45

Delete 方法中,将接收到的对象的元表设置为 nil,如果您稍后调用该对象的方法,您将收到一条错误消息。

In the Delete method, set the metatable of the received object to nil and you'll get an error message if you later call a method on that object.

你的呼吸 2025-01-10 05:28:45

我宁愿建议使用 SWIGLuaBind 相反,他们已经为你解决了这些陷阱。

I'd rather advice using SWIG or LuaBind instead, they've already taken care of such pitfalls for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文