Memory Management
native.memory provides capabilities for manual heap memory management. Please be mindful of memory leaks; manually allocated memory must be manually freed.
Allocate Memory (alloc)
Allocates a memory block of the specified size.
lua
native.memory.alloc(size) -> LuaPointer- Parameters:
size(number) —— Number of bytes. - Returns:
LuaPointerobject. Ifsize <= 0, returnsnilor a null pointer object.
Example:
lua
local ptr = native.memory.alloc(1024) -- Allocate 1KB
if ptr.is_null() then
log("Memory allocation failed")
endAllocate String Memory (alloc_utf8_string)
Allocates a block of memory and writes the Lua string into it encoded as UTF-8, automatically appending \0 at the end.
lua
native.memory.alloc_utf8_string(str) -> LuaPointer- Parameters:
str(string) —— Content. - Returns:
LuaPointerpointing to the start address of the string.
Example:
lua
local str_ptr = native.memory.alloc_utf8_string("Hello World")
-- Memory content: 48 65 6C 6C 6F 20 57 6F 72 6C 64 00Free Memory (free)
Frees memory allocated by the alloc family of functions.
lua
native.memory.free(ptr)- Parameters:
ptr(LuaPointer | number) —— The start address of the memory to free.
Warning:
- Only free memory allocated by yourself.
- Do not double free the same memory block.
- Do not free unallocated wild pointers.
Example:
lua
local buf = native.memory.alloc(16)
-- Use buf ...
native.memory.free(buf)