lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
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...

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGzfqWf9E0noFvlzgRAv4TAJ9Yz5I8PRlRGXscxz6hCyh9cxUt8QCgx1Om
KeBn0e9141zxvNw+uUqoXmc=
=qjyK
-----END PGP SIGNATURE-----