Skip to content

Commit aa7454e

Browse files
authored
fix: Searchkit memory leaks (#1735)
1 parent d69f23e commit aa7454e

File tree

6 files changed

+21
-22
lines changed

6 files changed

+21
-22
lines changed

CodeEdit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"originHash" : "41fcbec1ecbb7853d9ead798bba9d46f35f28767f4d41a009c8eeee022e99a84",
2+
"originHash" : "3f6921a5ec30d1ecb6d6b205cf27a816c318246bb00f0ea367b997cc66527d32",
33
"pins" : [
44
{
55
"identity" : "anycodable",
@@ -85,7 +85,7 @@
8585
{
8686
"identity" : "logstream",
8787
"kind" : "remoteSourceControl",
88-
"location" : "https://github.com/Wouter01/LogStream",
88+
"location" : "https://github.com/CodeEditApp/LogStream",
8989
"state" : {
9090
"revision" : "6f83694b2675dcf3b1cea0a52546ff4469c18282",
9191
"version" : "1.3.0"

CodeEdit/Features/Documents/Indexer/SearchIndexer+Add.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extension SearchIndexer {
2222
}
2323

2424
return modifyIndexQueue.sync {
25-
SKIndexAddDocumentWithText(index, document.takeUnretainedValue(), text as CFString, canReplace)
25+
SKIndexAddDocumentWithText(index, document.takeRetainedValue(), text as CFString, canReplace)
2626
}
2727
}
2828

@@ -66,7 +66,7 @@ extension SearchIndexer {
6666
let mime = mimeType ?? self.detectMimeType(fileURL)
6767

6868
return modifyIndexQueue.sync {
69-
SKIndexAddDocument(index, document.takeUnretainedValue(), mime as CFString?, canReplace)
69+
SKIndexAddDocument(index, document.takeRetainedValue(), mime as CFString?, canReplace)
7070
}
7171
}
7272

@@ -107,7 +107,7 @@ extension SearchIndexer {
107107
/// - Returns: `true` if the document was successfully removed, `false` otherwise.
108108
/// **Note:** If the document didn't exist, this also returns `true`.
109109
public func removeDocument(url: URL) -> Bool {
110-
let document = SKDocumentCreateWithURL(url as CFURL).takeUnretainedValue()
110+
let document = SKDocumentCreateWithURL(url as CFURL).takeRetainedValue()
111111
return self.remove(document: document)
112112
}
113113

CodeEdit/Features/Documents/Indexer/SearchIndexer+InternalMethods.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,24 @@ extension SearchIndexer {
6464

6565
var isLeaf = true
6666

67-
let iterator = SKIndexDocumentIteratorCreate(index, inParentDocument).takeUnretainedValue()
67+
let iterator = SKIndexDocumentIteratorCreate(index, inParentDocument).takeRetainedValue()
6868
while let skDocument = SKIndexDocumentIteratorCopyNext(iterator) {
6969
isLeaf = false
70-
self.addLeafURLs(index: index, inParentDocument: skDocument.takeUnretainedValue(), docs: &docs)
70+
self.addLeafURLs(index: index, inParentDocument: skDocument.takeRetainedValue(), docs: &docs)
7171
}
72-
7372
if isLeaf, inParentDocument != nil,
7473
kSKDocumentStateNotIndexed != SKIndexGetDocumentState(index, inParentDocument) {
75-
if let temp = SKDocumentCopyURL(inParentDocument) {
76-
let baseURL = temp.takeUnretainedValue() as URL
77-
let documentID = SKIndexGetDocumentID(index, inParentDocument)
78-
docs.append(
79-
DocumentID(
80-
url: baseURL,
81-
document: inParentDocument!,
82-
documentID: documentID
83-
)
74+
let url = SKDocumentCopyURL(inParentDocument).takeRetainedValue()
75+
76+
let documentID = SKIndexGetDocumentID(index, inParentDocument)
77+
docs.append(
78+
DocumentID(
79+
url: url as URL,
80+
document: inParentDocument!,
81+
documentID: documentID
8482
)
85-
}
83+
)
84+
8685
}
8786
}
8887

CodeEdit/Features/Documents/Indexer/SearchIndexer+ProgressiveSearch.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extension SearchIndexer {
9393

9494
let partialResult: [SearchResult] = zip(urls[0..<foundCount], scores)
9595
.compactMap { (cfurl, score) -> SearchResult? in
96-
guard let url = cfurl?.takeUnretainedValue() as URL? else {
96+
guard let url = cfurl?.takeRetainedValue() as URL? else {
9797
return nil
9898
}
9999

CodeEdit/Features/Documents/Indexer/SearchIndexer+Terms.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ extension SearchIndexer {
6767

6868
var result = [TermCount]()
6969

70-
let document = SKDocumentCreateWithURL(url as CFURL).takeUnretainedValue()
70+
let document = SKDocumentCreateWithURL(url as CFURL).takeRetainedValue()
7171
let documentID = SKIndexGetDocumentID(index, document)
7272

7373
guard let termVals = SKIndexCopyTermIDArrayForDocumentID(index, documentID),
74-
let terms = termVals.takeUnretainedValue() as? [CFIndex] else {
74+
let terms = termVals.takeRetainedValue() as? [CFIndex] else {
7575
return []
7676
}
7777

CodeEdit/Features/Documents/Indexer/SearchIndexer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class SearchIndexer {
6767
private(set) lazy var stopWords: Set<String> = {
6868
var stopWords: Set<String> = []
6969
if let index = self.index,
70-
let properties = SKIndexGetAnalysisProperties(self.index).takeUnretainedValue() as? [String: Any],
70+
let properties = SKIndexGetAnalysisProperties(self.index).takeRetainedValue() as? [String: Any],
7171
let newStopWords = properties[kSKStopWords as String] as? Set<String> {
7272
stopWords = newStopWords
7373
}

0 commit comments

Comments
 (0)