@@ -227,6 +227,7 @@ class Statement
227227 ' free ' : ->
228228 @ freemem ()
229229 res = sqlite3_finalize (@stmt ) is SQLite .OK
230+ delete @db .statements [@stmt ]
230231 @stmt = NULL
231232 return res
232233
@@ -240,7 +241,7 @@ class Database
240241 if data? then FS .createDataFile ' /' , @filename , data, true , true
241242 @ handleError sqlite3_open @filename , apiTemp
242243 @db = getValue (apiTemp, ' i32' )
243- @statements = [] # A list of all prepared statements of the database
244+ @statements = {} # A list of all prepared statements of the database
244245
245246 ### Execute an SQL query, ignoring the rows it returns.
246247
@@ -351,6 +352,7 @@ class Database
351352 stmt = @ [' prepare' ] sql, params
352353 while stmt[' step' ]()
353354 callback stmt[' getAsObject' ]()
355+ stmt[' free' ]()
354356 if typeof done is ' function' then done ()
355357
356358 ### Prepare an SQL statement
@@ -366,7 +368,7 @@ class Database
366368 if pStmt is NULL then throw ' Nothing to prepare'
367369 stmt = new Statement pStmt, this
368370 if params? then stmt .bind params
369- @statements . push stmt
371+ @statements [pStmt] = stmt
370372 return stmt
371373
372374 ### Exports the contents of the database to a binary array
@@ -386,7 +388,7 @@ class Database
386388 memory consumption will grow forever
387389 ###
388390 ' close ' : ->
389- stmt[' free' ]() for stmt in @statements
391+ stmt[' free' ]() for _, stmt of @statements
390392 @ handleError sqlite3_close_v2 @db
391393 FS .unlink ' /' + @filename
392394 @db = null
0 commit comments