开发者

Is something leaking from the following code

My app seems to be crashing on some line (console does not give exact idea). I just get Program received ERROR signal. Could you please let me know if there is some leak or other issue in the following code...I am releasing the properties in dealloc method;

- (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)) {
              开发者_StackOverflow中文版      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];
}

In dealloc, I am releasing;

[parse_error_str release];
    [replist_rptdt_dict release];
    [replist_rpttype_dict release];
    [curr_rep_date release];
    [aBook release];
    [tableView release];


If your curr_rep_date property is declared as retain or copy, then this is a leak:

self.curr_rep_date = [[NSMutableString alloc] init];

+alloc will return an instance you own, and the property is going to declare ownership as well (by sending retain to the array).

You should be doing:

self.curr_rep_date = [NSMutableString string];

In addition, doing things like:

tempArrDt = (NSMutableArray *)[tmpRptDt componentsSeparatedByString:@"-"];

Or

self.curr_rep_date = (NSMutableString *)[tmpRptDt stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%d",tmpYrVal] withString:[NSString stringWithFormat:@"%d",(tmpCurrYearInt-1)]];

Are totally wrong. Casting does not actually change the type of an object. It just shuts up the compiler. So -componentsSeparatedByString: will always return an immutable array, even if you cast it to an NSMutableArray (or anything else). Pay attention to return types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜