[英]Is something leaking from the following code
我的應用似乎在某些行崩潰了(控制台沒有給出確切的主意)。 我剛收到程序收到的ERROR信號。 您能否讓我知道以下代碼中是否存在泄漏或其他問題...我正在釋放dealloc方法中的屬性;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.curr_rep_date = [[NSMutableString alloc] init];
retrievedArray = [[Shared sharedManager] books];
NSString *urlstr_replist_curr = [[[NSString alloc] initWithFormat:@"http://xyz.com", [[retrievedArray objectAtIndex:selectedIndex] BookNumber]] autorelease];
NSData* xmlData_replist = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlstr_replist_curr] ];
replist_rptdt_dict = (NSMutableDictionary *)PerformXMLXPathQuery(xmlData_replist, @"//XX/YY[@RD]");
replist_rpttype_dict = (NSMutableDictionary *)PerformXMLXPathQuery(xmlData_replist, @"//XX/YY[@RT='A']");
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"YYYY"];
NSDate *date = [NSDate date];
int tmpCurrYearInt = [[dateFormatter stringFromDate:date] intValue];
NSString *tmpRptDt;
NSMutableArray *tempArrDt;
for (NSDictionary *period in replist_rpttype_dict){
for (NSDictionary *nodeAttributeArray in [period objectForKey:@"nodeAttributeArray"]){
NSString *tmpAttrName = [nodeAttributeArray objectForKey:@"attributeName"];
if ([tmpAttrName isEqualToString:@"ReportDate"]) {
tmpRptDt = [nodeAttributeArray objectForKey:@"nodeContent"];
tempArrDt = (NSMutableArray *)[tmpRptDt componentsSeparatedByString:@"-"];
tmpYrVal = [[tempArrDt lastObject] intValue];
if (tmpYrVal == tmpCurrYearInt) {
self.curr_rep_date = [NSString stringWithFormat:@"%d", tmpRptDt];
}
else if (tmpYrVal == (tmpCurrYearInt-1)) {
self.curr_rep_date = (NSMutableString *)[tmpRptDt stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%d",tmpYrVal] withString:[NSString stringWithFormat:@"%d",(tmpCurrYearInt-1)]];
}
else if (tmpYrVal == (tmpCurrYearInt-2)) {
self.curr_rep_date = (NSMutableString *)[tmpRptDt stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%d",tmpYrVal] withString:[NSString stringWithFormat:@"%d",(tmpCurrYearInt-2)]];
}
else {
parse_error_str = [NSMutableString stringWithString:@"Report error"];
}
}
}
}
NSLog(@"tmpRptDt is %@, Curr Rep date is %@",tmpRptDt, self.curr_rep_date);
[urlstr_replist_curr release];
[tableView reloadData];
}
在dealloc中,我正在釋放;
[parse_error_str release];
[replist_rptdt_dict release];
[replist_rpttype_dict release];
[curr_rep_date release];
[aBook release];
[tableView release];
如果您curr_rep_date
屬性被聲明為retain
或copy
,那么這是一個泄漏:
self.curr_rep_date = [[NSMutableString alloc] init];
+alloc
將返回您擁有的實例,並且該屬性還將聲明所有權(通過將retain
發送到數組)。
您應該這樣做:
self.curr_rep_date = [NSMutableString string];
另外,執行以下操作:
tempArrDt = (NSMutableArray *)[tmpRptDt componentsSeparatedByString:@"-"];
要么
self.curr_rep_date = (NSMutableString *)[tmpRptDt stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%d",tmpYrVal] withString:[NSString stringWithFormat:@"%d",(tmpCurrYearInt-1)]];
完全錯了。 強制轉換實際上不會更改對象的類型。 它只是關閉編譯器。 因此, -componentsSeparatedByString:
將始終返回不可變數組,即使將其NSMutableArray
轉換為NSMutableArray
(或其他任何東西)也是如此。 注意返回類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.