lua-users home
lua-l archive

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

Hash: SHA1

Shane Lee wrote:
> Yes, I figured that out around day three, however notice that I use a
> new pointer and assign the value to it instead of attempting to use
> the original value:
>   char *ret = (char *)lua_tostring(L, -1);
> So even if the L pointer itself is deleted or resized 'ret' retains
> it's value.

The value of ret isn't the problem --- it's the string data that it's pointing
at. That data is owned by Lua, and the garbage collector is at liberty to
remove it as soon as the pop happens. So you'll end up with a pointer to garbage.

It's one of those things were you can get away with it most of the time
(because the garbage collector's unlikely to run at that exact moment), but
the rest of the time you'll have completely undebuggable crashes. Best to
train yourself to do it correctly now...

- --
┌── ─── ───────────────────
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla -