Skip to content

Commit 2463732

Browse files
authored
Merge pull request #130 from moteus/multi_memory_leak
Fix. socket action callback prevents multi object from GC
2 parents f36ef25 + c9ed7c6 commit 2463732

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package = "Lua-cURL"
2+
version = "0.3.8-2"
3+
4+
source = {
5+
url = "https://github.com/Lua-cURL/Lua-cURLv3/archive/v0.3.8.zip",
6+
dir = "Lua-cURLv3-0.3.8",
7+
}
8+
9+
description = {
10+
summary = "Lua binding to libcurl",
11+
detailed = [[
12+
]],
13+
homepage = "https://github.com/Lua-cURL",
14+
license = "MIT/X11"
15+
}
16+
17+
dependencies = {
18+
"lua >= 5.1, < 5.4"
19+
}
20+
21+
external_dependencies = {
22+
platforms = {
23+
windows = {
24+
CURL = {
25+
header = "curl/curl.h",
26+
library = "libcurl",
27+
}
28+
};
29+
unix = {
30+
CURL = {
31+
header = "curl/curl.h",
32+
-- library = "curl",
33+
}
34+
};
35+
}
36+
}
37+
38+
build = {
39+
copy_directories = {'doc', 'examples', 'test'},
40+
41+
type = "builtin",
42+
43+
platforms = {
44+
windows = { modules = {
45+
lcurl = {
46+
libraries = {"libcurl", "ws2_32"},
47+
}
48+
}},
49+
unix = { modules = {
50+
lcurl = {
51+
libraries = {"curl"},
52+
}
53+
}}
54+
},
55+
56+
modules = {
57+
["cURL" ] = "src/lua/cURL.lua",
58+
["cURL.safe" ] = "src/lua/cURL/safe.lua",
59+
["cURL.utils" ] = "src/lua/cURL/utils.lua",
60+
["cURL.impl.cURL" ] = "src/lua/cURL/impl/cURL.lua",
61+
62+
lcurl = {
63+
sources = {
64+
"src/l52util.c", "src/lceasy.c", "src/lcerror.c",
65+
"src/lchttppost.c", "src/lcurl.c", "src/lcutils.c",
66+
"src/lcmulti.c", "src/lcshare.c", "src/lcmime.c",
67+
},
68+
incdirs = { "$(CURL_INCDIR)" },
69+
libdirs = { "$(CURL_LIBDIR)" }
70+
},
71+
}
72+
}
73+
74+

src/lua/cURL/impl/cURL.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,9 @@ local function wrap_callback(...)
670670
end
671671

672672
local function wrap_socketfunction(self, cb)
673+
local ptr = setmetatable({value = self},{__mode = 'v'})
673674
return function(h, ...)
674-
local e = self._easy[h]
675+
local e = ptr.value._easy[h]
675676
if e then return cb(e, ...) end
676677
return 0
677678
end

test/test_curl.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ local fname = "./test.download"
2020

2121
local utils = require "utils"
2222

23+
local function weak_ptr(val)
24+
return setmetatable({value = val},{__mode = 'v'})
25+
end
26+
27+
local function gc_collect()
28+
for i = 1, 5 do
29+
collectgarbage("collect")
30+
end
31+
end
32+
2333
-- Bug. libcurl 7.56.0 does not add `Content-Type: text/plain`
2434
local text_plain = utils.is_curl_eq(7,56,0) and 'test/plain' or 'text/plain'
2535

@@ -213,6 +223,38 @@ end
213223

214224
end
215225

226+
local _ENV = TEST_CASE'multi memory leak' if ENABLE then
227+
228+
local m
229+
230+
function teardown()
231+
if m then m:close() end
232+
m = nil
233+
end
234+
235+
function test_basic()
236+
local ptr do
237+
local multi = assert(scurl.multi())
238+
ptr = weak_ptr(multi)
239+
end
240+
gc_collect()
241+
242+
assert_nil(ptr.value)
243+
end
244+
245+
function test_socket_action()
246+
local ptr do
247+
local multi = assert(scurl.multi())
248+
multi:setopt_socketfunction(function() end)
249+
ptr = weak_ptr(multi)
250+
end
251+
gc_collect()
252+
253+
assert_nil(ptr.value)
254+
end
255+
256+
end
257+
216258
local _ENV = TEST_CASE'form' if ENABLE then
217259

218260
local post

0 commit comments

Comments
 (0)