Skip to content

Commit 8c00e98

Browse files
committed
Fixes for modern Swift
Storing a `Statement` no longer causes a crash when cleaning up. The `UPPER` function properly handles memory management of strings.
1 parent b9569fe commit 8c00e98

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 2.1.1 2023-09-07
9+
### Fixed
10+
- Storing a `Statement` no longer causes a crash when cleaning up.
11+
- The `UPPER` function properly handles memory management of strings.
12+
813
## 2.1.0 2020-08-16
914
### Added
1015
- `sqliteVersion` fetches the underlying SQLite version string.

Sources/Restructure/Restructure.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,10 @@ public class Restructure {
177177
}
178178

179179
let stringValue = String(cString: value).uppercased()
180-
sqlite3_result_text(context, stringValue, Int32(stringValue.utf8.count), SQLITE_STATIC)
180+
181+
stringValue.withCString { value in
182+
sqlite3_result_text(context, value, Int32(stringValue.utf8.count), SQLITE_TRANSIENT)
183+
}
181184
}, nil, nil)
182185
}
183186

@@ -225,6 +228,8 @@ public class Restructure {
225228
}
226229

227230
internal func finalize(statement: Statement) {
231+
guard preparedStatements.contains(statement.statement) else { return }
232+
228233
preparedStatements.remove(statement.statement)
229234

230235
let result = sqlite3_finalize(statement.statement)

Tests/RestructureTests/StatementTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ class StatementTests: XCTestCase {
2323
restructure = nil
2424
}
2525

26+
// MARK: - Finalzie Tests
27+
28+
func testFinalizeStoredStatement() throws {
29+
var restructure: Restructure? = try! Restructure()
30+
try! restructure!.execute(query: "CREATE TABLE foo (a INTEGER PRIMARY KEY AUTOINCREMENT)")
31+
32+
var statement: Statement? = try restructure!.prepare(query: "SELECT a FROM foo")
33+
34+
restructure?.close()
35+
restructure = nil
36+
37+
statement = nil
38+
39+
XCTSuccess("Completely freed stored statements")
40+
}
2641

2742
// MARK: - Prepare Tests
2843

0 commit comments

Comments
 (0)