问题
I new to profiling and using instruments in xcode. I am facing of problem of memory leaks in tableViewCell for UILabel(CALayer). In my code all the tableViewCell have a fixed view called bottomView. This bottomView may or may not contain UILabel which can be either directly within bottomView or within a subView of bottomView. Every time within the init method for cell I check for bottomView and remove it from superView and set it to nil. Then reinitialise the bottomView (using UIView(frame: )
) and then a switch case follows which adds contents to bottomView as required. I've set all other UILabels within the cell to nil and remove from superView in deinit which are fixed.
My questions and doubts:
- Is there a way to get where a particular UILabel created in code from Allocations screen or any other way to trace the object in code? Because I'm still only assuming that the leaks are from bottomView which have variable UILabels. Other than that I've removed all other UILabels from superView in both cell and viewController containing tableView.
- How to use "Record reference counts" setting in Allocation Record setting? I read at many places that it can be used to address the problem I'm facing in 2.
- Does removing bottomView from superView and setting it nil each time in init method before initialising new bottomView will ensure that all the subViews (be it any) gets removed too or is it required to search for UILabels with view.subviews() and set them to nil?
- I tried using Leaks tool although it does not show any red spike in the same generations I captured in Allocation screen. Why is it so? Which particular leaks are captured using Leaks pane.
- Just out of curiosity why do UILabels are the only which don't get deallocated automatically properly?
I'm new to debugging and memory management in iOS (particularly swift) so please excuse me if any of my questions are too obvious. Any help or links to useful resources regarding any of the above is welcome. Please let me know I am required to post any other resources relating to question.
EDIT 1: I cannot post the exact code, but this is roughly what I'm doing:
MyUITableViewCell.swift:
initializeCell() {
// Some initializations
if (self.viewWithTag(104) != nil) {
self.viewWithTag(104)!.removeFromSuperview()
}
bottomView = UIView(frame: CGRectMake(0, nextY + 8, self.frame.width, 10))
bottomView.tag = 104
self.addSubview(bottomView)
bottomType = someType
switch (bottomType) {
case 1:
someLabel = UILabel()
bottomView.addSubview(someLabel)
case 2:
someLabel = UILabel()
bottomView.addSubview(someLabel)
case 3:
someLabel = UILabel()
bottomView.addSubview(someLabel)
for x in array1:
someView = UIView()
someNewLabel = UILabel()
someView.addSubview(someNewLabel)
bottomView.addSubview(someView)
case 4:
someLabel = UILabel()
bottomView.addSubview(someLabel)
for x in array2:
someView = UIView()
someNewLabel = UILabel()
someView.addSubview(someNewLabel)
bottomView.addSubview(someView)
case 5:
someLabel1 = UILabel()
bottomView.addSubview(someLabel1)
someLabel2 = UILabel()
bottomView.addSubview(someLabel2)
someLabel3 = UILabel()
bottomView.addSubview(someLabel3)
default:
print("Unkown type")
}
//Some more initializations
}
来源:https://stackoverflow.com/questions/31570043/ios-debugging-memory-leaks-for-uilabel-in-swift