Page 3 of 3

Re: Problem using the 'SECS' class library

Posted: Fri Apr 19, 2013 11:15 pm
by Lafolie
A lot of work was put into Lua so that you don't have to do this kind of thing. The garbage collector is actually really effective in Lua, and if that's not enough then weak tables further prevent the need for destructors.

Re: Problem using the 'SECS' class library

Posted: Fri Apr 19, 2013 11:42 pm
by markgo
This is actually quite simple:

Code: Select all

test = {}
test.name = 'test'
function test:destroy()
  _G[self.name] = nil
end
test:destroy()
print(test)
Replace _G with a table that you're storing your object in.

Re: Problem using the 'SECS' class library

Posted: Fri Apr 19, 2013 11:43 pm
by kikito
Zephos wrote:Let Y be the main problem here now. My X should be irrelevant for you as long as I know exactly what I want, anyway. Just please give me the answer to my problem and let me be happy, especially if the solution is exactly as easy as I imagined it to be:

Code: Select all

function object_destroy(objstr)
	loadstring(objstr .. " = 'whateverthefuckiwant'")()
end

...

object_destroy("obj")
Now, is there anything to complain about this? In the end, that's all I wanted to know.
You can not do what you want. Let me try to explain this.

When you assign a variable to a value, for example local a = {}, you create a reference to it.
When you pass a variable to a function (for example by calling foo(a)), you are creating a second reference to the same value. While foo executes, your program has two references to the table. When foo finishes executing the second reference is eliminated, unless you return it or save it somewhere else.
This means that it doesn't matter what you do to the variable inside foo; the variable "outside" foo still exists - you are just modifying a second copy. In other words, your code is doing something equivalent to this:

Code: Select all

local a = {}
local b = a -- create a second reference
b = nil -- b is nil, but a still points to the table
Since your program still has one reference to the value, the garbage collector will not collect it (yet). It might collect the value later on, when all the references to it have been eliminated.

The simplest way to do what you want is using a table to contain the references. You can set the values of the table to nil very easily:

Code: Select all

local objects = {}
function object_destroy(objstr)
  objects[objstr] = "whateverthefuckiwant" -- no need to use loadString here
end

...

object_destroy("obj")

Re: Problem using the 'SECS' class library

Posted: Sat Apr 20, 2013 10:27 am
by Zephos
Excellent, that helped a lot. Thanks goes to all of you, and sorry for causing a bit of a fuss.