aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-05 00:45:43 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-05 00:45:43 +0930
commit66a4bafd43e4c8c418a7d699a0de0c608931b9c7 (patch)
tree2f5f30fc9bbc4fb40e922c44e6f4e52c79a4382b
parent238fa6368f30718f8fdb04caf3db406c62568cad (diff)
downloadlua-cjson-66a4bafd43e4c8c418a7d699a0de0c608931b9c7.tar.gz
lua-cjson-66a4bafd43e4c8c418a7d699a0de0c608931b9c7.tar.bz2
lua-cjson-66a4bafd43e4c8c418a7d699a0de0c608931b9c7.zip
Fix strbuf_t leak on lua_close()
- Add __gc metatable method to clean up json_config_t userdata.
-rw-r--r--lua_cjson.c23
1 files changed, 20 insertions, 3 deletions
diff --git a/lua_cjson.c b/lua_cjson.c
index c7d65af..c4c5a96 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -235,6 +235,18 @@ static int json_strict_numbers(lua_State *l)
235 return 1; 235 return 1;
236} 236}
237 237
238static int json_destroy_config(lua_State *l)
239{
240 json_config_t *cfg;
241
242 cfg = lua_touserdata(l, 1);
243 if (cfg)
244 strbuf_free(&cfg->encode_buf);
245 cfg = NULL;
246
247 return 0;
248}
249
238static void json_create_config(lua_State *l) 250static void json_create_config(lua_State *l)
239{ 251{
240 json_config_t *cfg; 252 json_config_t *cfg;
@@ -242,6 +254,14 @@ static void json_create_config(lua_State *l)
242 254
243 cfg = lua_newuserdata(l, sizeof(*cfg)); 255 cfg = lua_newuserdata(l, sizeof(*cfg));
244 256
257 /* Create GC method to clean up strbuf */
258 lua_newtable(l);
259 lua_pushcfunction(l, json_destroy_config);
260 lua_setfield(l, -2, "__gc");
261 lua_setmetatable(l, -2);
262
263 strbuf_init(&cfg->encode_buf, 0);
264
245 cfg->sparse_ratio = DEFAULT_SPARSE_RATIO; 265 cfg->sparse_ratio = DEFAULT_SPARSE_RATIO;
246 cfg->max_depth = DEFAULT_MAX_DEPTH; 266 cfg->max_depth = DEFAULT_MAX_DEPTH;
247 cfg->strict_numbers = 1; 267 cfg->strict_numbers = 1;
@@ -291,9 +311,6 @@ static void json_create_config(lua_State *l)
291 cfg->escape2char['r'] = '\r'; 311 cfg->escape2char['r'] = '\r';
292 cfg->escape2char['u'] = 'u'; /* Unicode parsing required */ 312 cfg->escape2char['u'] = 'u'; /* Unicode parsing required */
293 313
294 /* Encoding init */
295
296 strbuf_init(&cfg->encode_buf, 0);
297 314
298#if 0 315#if 0
299 /* Initialise separate storage for pre-generated escape codes. 316 /* Initialise separate storage for pre-generated escape codes.