Kitura/SwiftKueryMySQL

Fix memory leaks when queries return a `ResultSet`

kilnerm opened this issue · 1 comments

We have identified a couple of potential memory leaks within the SwiftKueryMySQL plugin.

Firstly if an error occurs in a call to QueryResult.asRows we can call back to the user completion handler without cleaning up the native resource associated with the embedded ResultSet.

Secondly if a user does not call done on a ResultSet the native resources will not be released.

In conclusion, we established that there is not a memory leak here: when the ResultSet goes out of scope and is deinitialized, the associated ResultFetcher (in this case, MySQLResultFetcher) will also be deinitialized. This already has a deinit that frees the native resources.

The contract of asRows was that the result set would always be freed, and Kitura/Swift-Kuery#178 addresses a failure to do this in the error case.

One remaining issue we identified is that, if a connection is to be reused following the production of a ResultSet, the native resources must be released before the connection executes another query. At the moment, those two things could happen concurrently if the user does not explicitly call done(), as execute is performed on another thread. I fear this might lead to weird timing related bugs. @kilnerm will raise a separate issue for this.