Skip to content

Commit 3c7412b

Browse files
Shapovalov KirillShapovalov Kirill
authored andcommitted
fix function pointer leak
1 parent 1a250c8 commit 3c7412b

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/api.coffee

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class Database
244244
@db = getValue(apiTemp, 'i32')
245245
RegisterExtensionFunctions(@db)
246246
@statements = {} # A list of all prepared statements of the database
247-
@functions = [] # A list of all user function (created by create_function call) of the database
247+
@functions = {} # A list of all user function of the database (created by create_function call)
248248

249249
### Execute an SQL query, ignoring the rows it returns.
250250
@@ -396,7 +396,8 @@ class Database
396396
###
397397
'export': ->
398398
stmt['free']() for _,stmt of @statements
399-
removeFunction(func) for func in @functions
399+
removeFunction(func) for _,func of @functions
400+
@functions={}
400401
@handleError sqlite3_close_v2 @db
401402
binaryDb = FS.readFile @filename, encoding:'binary'
402403
@handleError sqlite3_open @filename, apiTemp
@@ -416,7 +417,8 @@ class Database
416417
###
417418
'close': ->
418419
stmt['free']() for _,stmt of @statements
419-
removeFunction(func) for func in @functions
420+
removeFunction(func) for _,func of @functions
421+
@functions={}
420422
@handleError sqlite3_close_v2 @db
421423
FS.unlink '/' + @filename
422424
@db = null
@@ -481,9 +483,11 @@ class Database
481483
switch typeof(result)
482484
when 'number' then sqlite3_result_double(cx, result)
483485
when 'string' then sqlite3_result_text(cx, result, -1, -1)
484-
486+
if(name of @functions)
487+
removeFunction(@functions[name])
488+
delete @functions[name]
485489
# Generate a pointer to the wrapped, user defined function, and register with SQLite.
486490
func_ptr = addFunction(wrapped_func)
487-
@functions.push(func_ptr);
491+
@functions[name]=func_ptr
488492
@handleError sqlite3_create_function_v2 @db, name, func.length, SQLite.UTF8, 0, func_ptr, 0, 0, 0
489493
return @

0 commit comments

Comments
 (0)