lua-users home
lua-l archive

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


I'm having some trouble with userdata objects and I need some clarification
about what I'm doing...

In 4.0, Lua had lua_pushusertag(); this function would put a userdata object
on the stack.  If the value passed in as the userdata had not been
seen before, a new object was created. However, if the object had been
seen before, the old object was pushed onto the stack.

In 4.1, lua_pushusertag() has gone away, to be replaced by lua_newuserdata()
and lua_newuserdatabox().  However, the semantics are NOT the same. 

lua_newuserdata() always pushes a new value object onto the stack -- it does 
not re-use the old value object with the same tag/ptr.  This is causing
problems in code that used to work with 4.0 but no longer works with 4.1
because I'm getting multiple calls to the GC method for the same pointer.
(the first one deletes the object, the second one blows up because the
pointer is already free'd ;-(

What is the recommended course of action (maybe I just don't get 
"the model" ;-)

I have a small test program that illustrates the issue at:
http://www.fensende.com/~mcuddy/ltn/gctest.c

When I run the test program with 4.0:

D:\src\gctest>debug\gctest
Alloc object '781190' (object 1)
Alloc object '781030' (object 2)
got x=781030 (object 2)
destroy object 781030 (object 2)
destroy object 781190 (object 1)

When I run the test program with 4.1a:
D:\src\gctest>debug\gctest
Alloc object '782d00' (object 1)
Alloc object '782b30' (object 2)
got x=782b30 (object 2)
destroy object 782d00 (object 1)
destroy object 782b30 (object 2)
destroy object 782b30 (object 2)

--
Mike Cuddy (mcuddy@FensEnde.com, MC312), Programmer, Daddy, Human.
Fen's Ende Software, Redwood City, CA, USA, Earth, Sol System, Milky Way.

       Now I lay me down to sleep / CVS, I pray, my code to keep.  
       If disks crash before I wake: / format, newfs, cvs up, make.

       Join CAUCE: The Coalition Against Unsolicited Commercial E-mail.
                          <http://www.cauce.org/>