lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 7-Oct-06, at 2:16 AM, Nick Gammon wrote:

On 06/10/2006, at 9:29 AM, Rici Lake wrote:

And for a good reason. If you remove the 'self' argument from the stack, and there is no other reference to it, then it could get garbage collected while your method is running, with possibly disastrous consequences.

I'm trying to think how this would actually be a problem.

I have taken the information from the self parameter and stored it (in my case a userdata field) and then removed it from the stack, so I don't want it any more. The called function doesn't want it, as it never gets it in the first place.

I suppose it could happen, if you called the function with a method that was anonymously created on-the-fly, but it seems a strange way to code to me.

It's not the method that is created on the fly, it's the object. Depending on your problem domain, that can be quite common:

-- Bignum arithmetic (part of DSA signature)

local r = key.base:exptmod(seed, key.p):mod(key.q)
local s = hash(digest):addmod(key.private:invmod(key. q):mulmod(r, key.q),
                              key.q)
                      :mulmod(seed, key.q)


-- Creating hierarchical structures:
w = Window()
w:attach(
   Menubar()
      :add(Menu"File"
             :add(Menuitem"New")
             :add(Menuitem"Open")
             :add(Menuitem"Close")
             :add(Menudivider)
             :add(Menuitem"Print")
          )
      :add(Menu"Edit"

-- etc