If I include a locally created blob as parameter in a function call, directly or as an entry in a table, does it get passed by reference or is it copied into a new blob ? Example
local payload = PcUart.readblob(len);
calccrc = crc16(payload,0,len);
...
gEvents.SetEvent("HostCmd",{cmd = CmdId, status = "ok",payload = payload});
couple of questions
-
is the function call passing the reference of the blob, or is it copying the blob automatically into a new blob (by value)?
-
if by reference, does the GC destroy the blob as it goes out of scope (hence if the payload parameter is finally consumed it doesn’t necessary exist anymore)
-
if by value, what is the scope of that new blob (i.e. when does it get GCed) ?
-
is it needed to do an explicit copy e.g.
local payload = PcUart.readblob(len); calccrc = crc16(payload,0,len); ... gEvents.SetEvent("HostCmd",{cmd = CmdId, status = "ok",payload = payload.readblob(len)});
-
also here would be good to understand the lifecycle of the newly created blob through readblob() (when does it get GCed ?)
The latter is what I’m doing right now, but trying to avoid needless copying of data, as the blobs could become pretty large, but I’ve been bitten before by blobs being GCed before they could be consumed.