是从以下代码泄漏的东西

时间:2011-02-09 18:55:05

标签: iphone objective-c cocoa-touch

我的应用程序似乎在某些行崩溃(控制台没有给出确切的想法)。我只是得到程序收到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];

1 个答案:

答案 0 :(得分:3)

如果您的curr_rep_date属性被声明为retaincopy,那么这就是泄漏:

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(或其他任何内容)。注意返回类型。