[英]How to solve this memory leak?
+ (UITableViewCell *)inputCell {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
return cell;
}
Xcode 顯示 memory 泄漏。 我嘗試在單元初始化期間和返回期間自動釋放,但應用程序在這兩種情況下都崩潰了。
您發布的代碼將泄漏 memory 因為您的 alloc init 將返回一個保留計數為 1 的單元格。 大概調用代碼然后將此 object 返回到cellForRowAtIndexPath ,它將附加到UITableView並再次增加保留計數(到兩個)。 因此,當 UITableView 發布 memory 時,object 的保留計數仍為 1。
如果您嘗試在此代碼中自動釋放 object 並且它崩潰,那么您有一個單獨的錯誤。
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"] autorelease];
應該沒問題,除非您調用-inputCell
的代碼有問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.