> > > So are you saying that if - using your example - I do: > > local x = baz() > > that x can get collected out from under me? I don't think that's right. > I've always relied on cdata being collected when they're not in any GC > roots. > Well, certainly not while you're in the scope to which x is local, and not if you pass that Lua value out to a calling scope. But if you assign that Lua value to a struct member and don't return the Lua value to the caller then the object is free to be GC'd. In retrospect my example was poorly commented; what I meant to indicate in case 3 was that the constructor itself doesn't provide any anchoring; anchoring is the responsibility of the caller. As penance I have constructed a more complete and thorough set of examples, which should produce the following output (script attached): ~ $ luajit anchor.lua Constructing without any anchoring [Greetings dear friends! I am (0x0004d000)!] ---Forcing GC [Alas! I, (0x0004d000), have been collected.] Constructing with function-local anchoring [Greetings dear friends! I am (0x000431d0)!] ---Forcing GC The object sthould still be alive. Releasing local anchor [Alas! I, (0x000431d0), have been collected.] Constructing with function-local anchoring ... but not storing returned reference in caller scope [Greetings dear friends! I am (0x00049250)!] ---Forcing GC [Alas! I, (0x00049250), have been collected.] Constructing with caller-local anchoring [Greetings dear friends! I am (0x00043a30)!] ---Forcing GC [Alas! I, (0x00043a30), have been collected.] Releasing caller anchors and forcing GC Fin Demetri
Attachment:
anchor.lua
Description: Binary data