Guest User

Untitled

a guest
Nov 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // can be made dry by adding a alloc/dealloc parameter
  2. // add the DEBUGALLOC flag to Active Compilation Conditions -> Debug and use it only when necessary.
  3. // note that you should be using Instruments for this, but this helps to catch issues in real time when running the app, and I gnenerally don't recommend following this approach for several reasons.
  4.  
  5.  
  6. func LogDealloc<T>(from caller:T, filename: String = #file, line: Int = #line, funcname: String = #function){
  7. #if DEBUGALLOC
  8. let file = ("\(filename)" as NSString).lastPathComponent as String
  9. let objName = String(describing: type(of: caller))
  10. let objAddress = Unmanaged.passUnretained(caller as AnyObject).toOpaque()
  11. var logMessage = "[DEBUG] [DEALLOC] [\(file):\(line)] \(objName) @ \(objAddress)"
  12. print(logMessage)
  13. #endif
  14. }
  15.  
  16. func LogAlloc<T>(from caller:T, filename: String = #file, line: Int = #line, funcname: String = #function){
  17. #if DEBUGALLOC
  18. let file = ("\(filename)" as NSString).lastPathComponent as String
  19. let objName = String(describing: type(of: caller))
  20. let objAddress = Unmanaged.passUnretained(caller as AnyObject).toOpaque()
  21. var logMessage = "[DEBUG] [ALLOC] [\(file):\(line)] \(objName) @ \(objAddress)"
  22. print(logMessage)
  23. #endif
  24. }
  25.  
  26. //usage:
  27.  
  28. class MyObject {
  29. init(){
  30. LogAlloc(from: self)
  31. }
  32.  
  33. deinit{
  34. LogDealloc(from: self)
  35. }
  36. }
  37.  
  38. // output is something like this:
  39. /*
  40. [DEBUG] [ALLOC] [MyObject.swift:3] MyObjectOrInheritedOrProtocol @ 0x000060000154dca0
  41. [DEBUG] [DEALLOC] [MyObject.swift:7] MyObjectOrInheritedOrProtocol @ 0x000060000154dca0
  42. */
Add Comment
Please, Sign In to add comment