brimworks/lua-zlib

Memory leak

flashjay opened this issue · 2 comments

I'm using lua-zlib as shared library (zlib.so)

local zlib = require "zlib"
local stream = zlib.deflate()
local deflated, eof, bytes_in, bytes_out = stream(str, "finish")
...

everything goes fine, but lz_deflate_delete and lz_inflate_delete never be called, and it may raise memory leak ?

[test on Lua 5.3]

The lz_deflate_delete and lz_inflate_delete functions should be called when the closure (in your code above the stream object) gets garbage collected, since one of the upvalues of that closure is the "lz.deflate.meta" metatable which has a __gc metamethod. Note that if you read to the end of stream, then the deflateEnd call is made by lz_filter_impl (the function point called "end" contains deflateEnd) and therefore the memory can be reclaimed quicker.

Are you sure the stream is dropping out of scope in your code? Also note that the garbage collector tends to be lazy, so to verify if those __gc metamethods are not getting called, you should make an explicit call to collectgarbage().

Thank you. My fault.