From 4dc67f44e3b4ae34b0d72d576d5bd16b307e0a20 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Wed, 8 Jun 2016 18:38:26 -0400 Subject: [PATCH] fix pointer crash --- Sources/SQLite.swift | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Sources/SQLite.swift b/Sources/SQLite.swift index a2e004a..0b07826 100644 --- a/Sources/SQLite.swift +++ b/Sources/SQLite.swift @@ -9,10 +9,7 @@ let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self) public class SQLite { typealias PrepareClosure = ((SQLite) throws -> ()) - private var statementPointer: UnsafeMutablePointer! = nil - private var statement: OpaquePointer { - return statementPointer.pointee! - } + private var statement: OpaquePointer? var database: OpaquePointer? @@ -45,12 +42,22 @@ public class SQLite { func execute(_ queryString: String, prepareClosure: PrepareClosure = { _ in }) throws -> [Result.Row] { bindPosition = 0 - statementPointer = UnsafeMutablePointer.init(allocatingCapacity: 1) + + let statementPointer = UnsafeMutablePointer.init(allocatingCapacity: 1) + defer { + statementPointer.deinitialize() + self.statement = nil + } if sqlite3_prepare_v2(database, queryString, -1, statementPointer, nil) != SQLITE_OK { throw Error.prepare(errorMessage) } + guard let statement = statementPointer.pointee else { + return [] + } + self.statement = statement + try prepareClosure(self) var result = Result() @@ -126,20 +133,32 @@ public class SQLite { } func bind(_ value: Double) throws { - if sqlite3_bind_double(statementPointer.pointee, nextBindPosition, value) != SQLITE_OK { + guard let statement = self.statement else { + return + } + + if sqlite3_bind_double(statement, nextBindPosition, value) != SQLITE_OK { throw Error.bind(errorMessage) } } func bind(_ value: Int) throws { - if sqlite3_bind_int(statementPointer.pointee, nextBindPosition, Int32(value)) != SQLITE_OK { + guard let statement = self.statement else { + return + } + + if sqlite3_bind_int(statement, nextBindPosition, Int32(value)) != SQLITE_OK { throw Error.bind(errorMessage) } } func bind(_ value: String) throws { + guard let statement = self.statement else { + return + } + let strlen = Int32(value.characters.count) - if sqlite3_bind_text(statementPointer.pointee, nextBindPosition, value, strlen, SQLITE_TRANSIENT) != SQLITE_OK { + if sqlite3_bind_text(statement, nextBindPosition, value, strlen, SQLITE_TRANSIENT) != SQLITE_OK { throw Error.bind(errorMessage) } }