Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jgodon
Last active October 18, 2017 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgodon/0cd914e86151ecf0ed2714467524e084 to your computer and use it in GitHub Desktop.
Save jgodon/0cd914e86151ecf0ed2714467524e084 to your computer and use it in GitHub Desktop.
Memory leaks πŸ’¦, breakpoints & viewControllers (https://pagesjaunestech.ghost.io/viewcontrollers-leaks-breakpoints-magiques/)
struct AllocDeallocTester {
struct AllocationResult : CustomDebugStringConvertible {
let className : String
var pointerNumber : Int = 0
var numberOfMissDealloc : Int = 0
var numberOfValidDealloc : Int = 0
init(className inputClassName: String) {
self.className = inputClassName
}
var debugDescription: String {
get {
let state = numberOfMissDealloc == 0 ? "πŸ‘πŸ»" : "πŸ‘ŽπŸ»"
return "\n\(state)\(className)[\(pointerNumber)] nonDealloc(\(numberOfMissDealloc)) Dealloc(\(numberOfValidDealloc))"
}
}
}
enum AllocType {
case alloc
case dealloc
}
struct LigneResult {
let className : String
let pointer : String
var type : AllocType = .alloc
}
static func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = text as NSString
let range = NSRange(location: 0, length: nsString.length)
let results = regex.matches(in: text, options: [], range: range)
var output : [String] = []
output.append(nsString.substring(with: (results.first?.range(at: 1))!))
output.append(nsString.substring(with: (results.first?.range(at: 2))!))
output.append(nsString.substring(with: (results.first?.range(at: 3))!))
return output
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
static func getLigneData(inputString : String) -> LigneResult? {
let regexPattern = "-- (alloc|dealloc) \\d+ -- @\"<(.*): (.*)>"
let res = matches(for: regexPattern, in: inputString)
if res.count != 3 {
return nil
}
let alloc = (res[0] == "dealloc") ? AllocType.dealloc : AllocType.alloc
let className = res[1]
let pointer = res[2]
return LigneResult(className: className, pointer: pointer, type: alloc)
}
static func filterByPointer(lignes : [LigneResult]) -> [String:[LigneResult]] {
var pointerOriented : [String:[LigneResult]] = [:]
for aLigne in lignes {
if let _ = pointerOriented[aLigne.pointer] {
pointerOriented[aLigne.pointer]?.append(aLigne)
} else {
pointerOriented[aLigne.pointer] = [aLigne]
}
}
return pointerOriented
}
static func filterByClassName(lignes : [LigneResult]) -> [String:[String : [LigneResult]]] {
var pointerOriented : [String:[LigneResult]] = [:]
for aLigne in lignes {
if let _ = pointerOriented[aLigne.className] {
pointerOriented[aLigne.className]?.append(aLigne)
} else {
pointerOriented[aLigne.className] = [aLigne]
}
}
var res : [String:[String : [LigneResult]]] = [:]
for (className, lignes) in pointerOriented {
res[className] = filterByPointer(lignes :lignes)
}
return res
}
static func transformLigneDataToResult(lignes : [LigneResult]) -> [AllocationResult] {
var output : [AllocationResult] = []
let filteredLignes = filterByClassName(lignes: lignes)
for (className, pointerLigne) in filteredLignes {
var allocR = AllocationResult(className: className)
allocR.pointerNumber = pointerLigne.count
var numberOfValidDealloc = 0
var numberOfMissDealloc = 0
for (pointer, lignes) in pointerLigne {
let allocs = lignes.reduce(0, { (res, l) in
if (l.type == .alloc) {
return res + 1
}
return res + 0
})
let deallocs = lignes.reduce(0, { (res, l) in
if (l.type == .dealloc) {
return res + 1
}
return res + 0
})
if allocs - deallocs == 0 {
numberOfValidDealloc = numberOfValidDealloc + 1
} else {
numberOfMissDealloc = numberOfMissDealloc + 1
}
}
allocR.numberOfValidDealloc = numberOfValidDealloc
allocR.numberOfMissDealloc = numberOfMissDealloc
output.append(allocR)
}
return output
}
public static func generateAllocationResult(inputTexte : String) -> [AllocationResult] {
let lines = inputTexte.components(separatedBy: .newlines)
let results = lines.flatMap(getLigneData)
let allocationsStats = transformLigneDataToResult(lignes : results)
return allocationsStats
}
}
let res = AllocDeallocTester.generateAllocationResult(inputTexte: inputAllocation)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment