2014-09-19 23:23 GMT+04:00 Cosmin Apreutesei <cosmin.apreutesei@xxxxxxxxx>: > Is it possible that you're hitting undefined > behavior and just escaped a crash in this particular example? > No, on Linux x86-64 the ABI for a scalar and a struct wrapping scalar is the same (likely so on any ABI as there are little reasons to do otherwise). I faked malloc/sin just for the sake of example, I don't know any universally available function that returns a struct by value. Also note that accessing struct value (in example with sin()) and other metatable methods (__len) work as expected, so ABI part is safe (and just for the case I actually checked it right now with the real function returning a struct :D - works the same way). As has been cited earlier, the docs say "The __gc metamethod only applies to struct/union types and performs an implicit ffi.gc() <http://luajit.org/ext_ffi_api.html#ffi_gc> call during creation of an instance." So my guess that at some moment this particular case has been overlooked and no internal ffi.gc() call has been added for structs returned by value, though they are likely boxed with the code sequence similar to ffi.new() call. Other metamethods work because they also have to work for pointer to struct and hence take a different code path. -- Tomash Brechko