> No, on the account that the string is still registered. A Lua string will be created at the start of the Lua state and that string will remain referenced within Lua and thus never garbage collected.
This relies on implementation details that are not explicitly documented as design guarantees. To the best of my knowledge, nowhere does the documentation indicate that it is OK to use pointers to interned strings when a corresponding string value is not on the API stack. Secondly, the registry entry might be intentionally or mistakenly overwritten.
This is why I deem this brittle.
> What do you mean popping prematurely?
Popping a string value off the API stack before you are done using the corresponding string pointer.
> A C function has to ensure the stack is balanced before returning, right?
No. It is "considered good programming practice", quoting the manual, and said only in the context of a caller, not a callee.
> And that means popping before returning whatever was pushed during the function call.
No. "Whenever Lua calls C, the called function gets a new stack, which is independent of previous stacks and of stacks of C functions that are still active." So a callee can leave the stack in whatever state. All it needs to do is indicate, via its return value, how many values at the top of the stack are its return values. "Any other value in the stack below the results will be properly discarded by Lua." Quoted sentences courtesy of the fine manual.
> There is no way to directly grab a string value from Lua memory; you have to first push the string onto the stack then call lua_tostring() and as a consequence you then have to pop it back off before returning.
It would not hurt to pop it before returning, but, again, this is not required.