面向对象的Lua类泄漏 [英] Object oriented lua classes leaking
问题描述
我已经阅读了很多有关面向对象的lua(设置元表)的信息,并且已经构建了一个带有继承功能的系统.
我的问题是,某些变量似乎相互泄漏.如果我调用一个名为window:click(x, y)
的函数,该函数调用就可以了.此功能的工作是通知我的所有组件单击.它在做什么
for number, component in pairs(self.components) do
component.focus = false
component:click(x, y, msg)
end
self.components
包含窗口的所有组件
要充当所有组件的基类,我有一个名为component.lua的类,此文件创建一个名为components的表,并向其中添加一个create()
方法(该方法执行所有常见的OO lua任务),基本类包含我想要的所有组件中的所有方法和变量,包括component:click(x, y)
,并且再次被调用.
for key, callback in pairs(self.clickCallback) do
callback()
end
return
clickCallback
表包含在通知组件时应调用的函数.并在component.lua
从这里,只需设置新组件(文本框,按钮,标签等)的元表,就可以将该类继承到其他类.这些组件就是添加到窗口中的self.components
表中的内容.
问题在于,这些组件中的每个组件都应具有自己的clickCallback表.我正在通过component.lua
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
但是当我在一个组件上调用click(x,y)
时,它将调用所有clickHandler,无论是用于另一个按钮还是用于标签.
正如您在上面看到的那样,我正在设置一个名为focus
的参数,这似乎遇到了相同的问题,在其中为一个组件设置它(如您所见,我正在遍历每个组件)为所有组件设置它(因此,如果我有4个组件,则每个组件的焦点都将重置4次)
为什么lua会这样做,该如何解决?
首先,要想从您的小摘要中找出正在发生问题的完整示例,要想弄清楚到底发生了什么,要困难得多.
>
self.components包含窗口的所有组件
这可能是您在这里遇到的问题.再一次,这是一个猜测,因为您没有显示create
方法,但是如果您的构造函数没有为每个 instance 初始化clickCallback
成员,那么它将使用表中的类本身.
以下是说明问题的示例:
component = {}
component.__index = component
component.clickCallback = {}
function component.create()
return setmetatable({}, component)
end
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
function component:click(x,y)
for _,callback in pairs(self.clickCallback) do
callback(x,y)
end
end
a = component.create()
b = component.create()
a:addClickHandler(function(x,y) print("a", x, y) end)
b:addClickHandler(function(x,y) print("b", x, y) end)
a:click(10,20)
b:click(11,22)
以下是输出,其中显示了您描述的症状:
a 10 20
b 10 20
a 11 22
b 11 22
换句话说,调用a:click
会调用a
和b
的处理程序,因为clickCallback
表位于类本身中,并由该类的所有实例共享.该修补程序可确保每个实例都有自己的处理程序表:
component = {}
component.__index = component
function component.create()
return setmetatable({ clickCallback = {}}, component)
end
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
function component:click(x,y)
for _,callback in pairs(self.clickCallback) do
callback(x,y)
end
end
a = component.create()
b = component.create()
a:addClickHandler(function(x,y) print("a", x, y) end)
b:addClickHandler(function(x,y) print("b", x, y) end)
a:click(10,20)
b:click(11,22)
输出:
a 10 20
b 11 22
I've read quite a bit about object oriented lua (setting the metatable), and i have constructed a system, complete with inheritance.
My problem is now, that some of the variables seem to be leaking into each other. if i call a function called window:click(x, y)
the function calls just fine. the job of this function is the notify all my components of a click. which it is doing
for number, component in pairs(self.components) do
component.focus = false
component:click(x, y, msg)
end
self.components
contains all the components of the window
To act as a baseclass for all the components i have a class called component.lua, this file creates a table called components, and adds a create()
method to it (that does all the usual OO lua stuff), this baseclass contains all the methods and variables i want in all my components, including component:click(x, y)
and once again, it get's called.
for key, callback in pairs(self.clickCallback) do
callback()
end
return
the clickCallback
table contains functions that should be called when the component is notified. and is initialized inside component.lua
From here i inherit this class over to my other classes, simply by setting my metatable of my new component (textbox, button, label etc.). These components are what get's added to the self.components
table in the window.
The problem is that each of these components should have their own clickCallback table. Which i am writing to via a setter in component.lua
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
but when i call click(x,y)
on one component it calls all the clickHandlers, whether that be for another button or a label.
As you could see above, i am setting a parameter called focus
this seems to experience the same problem, where setting it for one component (as you can see i am looping through each of them) sets it for all (so if i have 4 components the focus gets reset 4 times on each component)
Why is lua doing this, and what can be done to fix it?
First, it's vastly more difficult to try to figure out what's going on from your little snippets that if you just posted a complete working example demonstrating the problem.
self.components contains all the components of the window
This is probably your problem right here. Again, this is a guess, because you didn't show the create
method, but if your constructor is not initializing a clickCallback
member for each instance, then it's going to use the table in the class itself.
Here's an example illustrating the problem:
component = {}
component.__index = component
component.clickCallback = {}
function component.create()
return setmetatable({}, component)
end
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
function component:click(x,y)
for _,callback in pairs(self.clickCallback) do
callback(x,y)
end
end
a = component.create()
b = component.create()
a:addClickHandler(function(x,y) print("a", x, y) end)
b:addClickHandler(function(x,y) print("b", x, y) end)
a:click(10,20)
b:click(11,22)
Here's the output, which shows the symptom you described:
a 10 20
b 10 20
a 11 22
b 11 22
In other words, calling a:click
calls the handler for a
and b
, because the clickCallback
table is in the class itself, being shared by all instances of that class. The fix it make sure each instance has it's own handler table:
component = {}
component.__index = component
function component.create()
return setmetatable({ clickCallback = {}}, component)
end
function component:addClickHandler(handler)
table.insert(self.clickCallback, handler)
end
function component:click(x,y)
for _,callback in pairs(self.clickCallback) do
callback(x,y)
end
end
a = component.create()
b = component.create()
a:addClickHandler(function(x,y) print("a", x, y) end)
b:addClickHandler(function(x,y) print("b", x, y) end)
a:click(10,20)
b:click(11,22)
Output:
a 10 20
b 11 22
这篇关于面向对象的Lua类泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!