Skip to content

Commit

Permalink
Fix memory leak with Vertex. (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored Mar 2, 2023
1 parent 1384aa6 commit 15b7df0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 11 additions & 3 deletions Sources/Megrez/2_Walker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public extension Megrez.Compositor {
}

let terminal = Vertex(node: .init(keyArray: ["_TERMINAL_"]))
var root = Vertex(node: .init(keyArray: ["_ROOT_"]))

for (i, vertexSpan) in vertexSpans.enumerated() {
for vertex in vertexSpan {
Expand All @@ -46,27 +47,34 @@ public extension Megrez.Compositor {
}
}

let root = Vertex(node: .init(keyArray: ["_ROOT_"]))
root.distance = 0
root.edges.append(contentsOf: vertexSpans[0])

var ordered: [Vertex] = topologicalSort(root: root)
var ordered = topologicalSort(root: &root)
for (j, neta) in ordered.reversed().enumerated() {
for (k, _) in neta.edges.enumerated() {
relax(u: neta, v: &neta.edges[k])
}
ordered[j] = neta
}

var iterated = terminal
var walked = [Node]()
var totalLengthOfKeys = 0
var iterated = terminal

while let itPrev = iterated.prev {
walked.append(itPrev.node)
iterated = itPrev
totalLengthOfKeys += iterated.node.spanLength
}

// 清理內容,否則會有記憶體洩漏。
ordered.removeAll()
vertexSpans.removeAll()
iterated.destroy()
root.destroy()
terminal.destroy()

guard totalLengthOfKeys == keys.count else {
print("!!! ERROR A")
return (result, false)
Expand Down
12 changes: 11 additions & 1 deletion Sources/Megrez/5_Vertex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ extension Megrez.Compositor {
public init(node: Node) {
self.node = node
}

/// 讓一個 Vertex 順藤摸瓜地將自己的所有的連帶的 Vertex 都摧毀,再摧毀自己。
/// 此過程必須在一套 Vertex 全部使用完畢之後執行一次,可防止記憶體洩漏。
public func destroy() {
while prev?.prev != nil { prev?.destroy() }
prev = nil
edges.forEach { $0.destroy() }
edges.removeAll()
node = .init()
}
}

/// 卸勁函式。
Expand Down Expand Up @@ -67,7 +77,7 @@ extension Megrez.Compositor {
/// 至於其遞迴版本,則類似於 Cormen 在 2001 年的著作「Introduction to Algorithms」當中的樣子。
/// - Parameter root: 根頂點。
/// - Returns: 排序結果(頂點陣列)。
func topologicalSort(root: Vertex) -> [Vertex] {
func topologicalSort(root: inout Vertex) -> [Vertex] {
class State {
var iterIndex: Int
let vertex: Vertex
Expand Down

0 comments on commit 15b7df0

Please sign in to comment.