Skip to content

Commit

Permalink
Merge pull request #98 from iZettle/experiment-saving-vc-memory-leaks…
Browse files Browse the repository at this point in the history
…-into-json-files

Save `json` files with `UIViewController` memory leak name
  • Loading branch information
bartszczepaniak authored Feb 22, 2024
2 parents 0ef9f76 + b6da883 commit 83608f2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.17.0
- Save `json` files with `UIViewController` memory leak name

# 1.16.0
- Fix split view crash for iOS 17

Expand Down
79 changes: 79 additions & 0 deletions Presentation/MemoryUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public let enabledDisplayAlertOnMemoryLeaksKey = "enabledDisplayAlertOnMemoryLea

func onLeak() {
log(.didLeak(.init(vcPresentationDescription), from: .init(presentationDescription)))
let memoryLeak = UIVCMemoryLeak(name: self.presentationTitle)
UIVCMemoryLeakFileWriter().write(memoryLeak: memoryLeak)

guard UserDefaults.standard.bool(forKey: enabledDisplayAlertOnMemoryLeaksKey) else { return }

Expand Down Expand Up @@ -75,3 +77,80 @@ extension NSObjectProtocol {
}

private var memoryLeakTrackingEnabledKey = false

private struct UIVCMemoryLeak {

internal let name: String

internal func jsonRepresentation() -> [String: Any] {
return [
"name": self.name
]
}
}

private class UIVCMemoryLeakFileWriter {

private let mlDirName = "presentation-memory-leaks"
private let tmpDir = "/tmp"

internal func write(memoryLeak: UIVCMemoryLeak) {
let mlDirPath = self.tmpDir.appending("/\(self.mlDirName)")
self.createMemoryLeaksDir(pathToDir: mlDirPath)
let fileName = UUID().uuidString + ".json"
let filePath = mlDirPath.appending("/\(fileName)")
let fileURL = URL(fileURLWithPath: filePath)
let jsonWriter = JSONWriter(jsonPath: fileURL)
do {
try jsonWriter.writeJSON(memoryLeak.jsonRepresentation())
} catch {
print("| Presentation >>> cannot write JSON file for memory leak ✗")
}
}

private func createMemoryLeaksDir(pathToDir: String) {
do {
let mlDirURL = URL(
fileURLWithPath: pathToDir
)
try FileManager.default.createDirectory(
at: mlDirURL,
withIntermediateDirectories: true
)
} catch {
print("| Presentation >>> cannot create dir ✗ : \(self.mlDirName)")
print(error)
}
}
}

private class JSONWriter {

private var jsonPath: URL

internal init(jsonPath: URL) {
self.jsonPath = jsonPath
}

internal func writeJSON(_ json: [String: Any]) throws {
let fileName = self.jsonPath.lastPathComponent
let jsonData = try JSONSerialization.data(
withJSONObject: json,
options: [.prettyPrinted]
)
guard let jsonString = String(data: jsonData, encoding: .utf8) else {
print("| Presentation >>> write ✗ : \(fileName)")
throw JSONWriterError.cannotWriteGivenFileAsJSON
}
try jsonString.write(
to: self.jsonPath,
atomically: true,
encoding: .utf8
)
print("| Presentation >>> write ✓ : \(fileName)")
}
}

private enum JSONWriterError: Error {
case cannotWriteGivenFileAsJSON
}

0 comments on commit 83608f2

Please sign in to comment.