@@ -244,6 +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 of the database (created by create_function call)
247248
248249 ### Execute an SQL query, ignoring the rows it returns.
249250
@@ -395,6 +396,8 @@ class Database
395396 ###
396397 ' export ' : ->
397398 stmt[' free' ]() for _,stmt of @statements
399+ removeFunction (func) for _,func of @functions
400+ @functions = {}
398401 @ handleError sqlite3_close_v2 @db
399402 binaryDb = FS .readFile @filename , encoding : ' binary'
400403 @ handleError sqlite3_open @filename , apiTemp
@@ -414,6 +417,8 @@ class Database
414417 ###
415418 ' close ' : ->
416419 stmt[' free' ]() for _,stmt of @statements
420+ removeFunction (func) for _,func of @functions
421+ @functions = {}
417422 @ handleError sqlite3_close_v2 @db
418423 FS .unlink ' /' + @filename
419424 @db = null
@@ -478,8 +483,11 @@ class Database
478483 switch typeof (result)
479484 when ' number' then sqlite3_result_double (cx, result)
480485 when ' string' then sqlite3_result_text (cx, result, - 1 , - 1 )
481-
486+ if (name of @functions )
487+ removeFunction (@functions [name])
488+ delete @functions [name]
482489 # Generate a pointer to the wrapped, user defined function, and register with SQLite.
483490 func_ptr = addFunction (wrapped_func)
491+ @functions [name]= func_ptr
484492 @ handleError sqlite3_create_function_v2 @db , name, func .length , SQLite .UTF8 , 0 , func_ptr, 0 , 0 , 0
485493 return @
0 commit comments