brimworks / lua-zlib

Simple streaming interface to zlib for Lua.
273 stars 111 forks source link

Memory leak #29

Closed flashjay closed 9 years ago

flashjay commented 9 years ago

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]

brimworks commented 9 years ago

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().

flashjay commented 9 years ago

Thank you. My fault.