释放thread1 exc_bad_access

时间:2012-08-24 07:04:33

标签: ios uitableview thread-safety exc-bad-access nsautoreleasepool

我是IOS编程的新手,我的程序运行正常,但我发现它有内存韭菜,所以我开始释放对象。

当我现在启动程序时,它会给我一个错误:

@autoreleasepool {
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
} 

和:

Thread 1: EXC_BAD_ACCESS (code = 1, address = 0x3f800010)

我尝试调试它,我发现该程序在创建tableView时崩溃了。 它创建了整个第一部分,但在第二部分的第二行中,它在返回的行中崩溃。

这是我创建表的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



[tableView setBackgroundView:nil];
tableView.backgroundColor = [UIColor clearColor];





    // Configure the cell...

    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            static NSString *CellIdentifier = @"TitleCell";
            UILabel *title;

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            cell = nil;
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"TitleCell" owner:self options:nil];
                titleCell.layer.masksToBounds = YES;
                titleCell.layer.cornerRadius =0.0;
                cell = titleCell;
                self.titleCell = nil;


            }

            title =(UILabel *)[cell viewWithTag:1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            NSString *orderString=[[[NSString alloc] init] autorelease];





                    title.text = [[titles objectAtIndex:indexPath.section] objectAtIndex:2];
                    cell.accessoryView = nil;



            if (!isPad()) {
                [cell.contentView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.18 blue:0.24 alpha:1]];
            }

            return cell;
        }
        else
        {
            // all other rows

            static NSString *CellIdentifier = @"DataCell";
            UILabel *title;
            UILabel *update;
            UILabel *download;
            UILabel *updateText;
            UILabel *downloadText;

            UIImageView *favoriteIcon;




            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            cell = nil;
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"DataCell" owner:self options:nil];
                cell = dataCell;
                self.dataCell = nil;


            }


            cell.layer.cornerRadius =0;
            title =(UILabel *)[cell viewWithTag:1];
            download = (UILabel *)[cell viewWithTag:3];
            update = (UILabel *)[cell viewWithTag:2];
            favoriteIcon = (UIImageView *)[cell viewWithTag:4];
            updateText = (UILabel *)[cell viewWithTag:5];
            downloadText = (UILabel *)[cell viewWithTag:6];

            updateText.text = NSLocalizedString(@"updated", nil);
            downloadText.text = NSLocalizedString(@"downloaded", nil);

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"MM. d. YYYY"];
            starIcone = favoriteIcon;
            int indicator = 0;


                    for (int i=0; i<[allData count]; i++) {
                        if ([[[allData objectAtIndex:i] objectAtIndex:1] isEqualToNumber:[[titles objectAtIndex:indexPath.section] objectAtIndex:0]] ) {
                            indicator++;
                        }
                        if (indicator == indexPath.row) {



                            title.text = [[allData objectAtIndex:i] objectAtIndex:2];
                            download.text = [dateFormat stringFromDate:[self db_get_date:[[[allData objectAtIndex:i] objectAtIndex:0]intValue]]];
                            update.text = [dateFormat stringFromDate:[[allData objectAtIndex:i] objectAtIndex:3]];





                            break;
                        }
                    }






            [dateFormat release];

            [favoriteIcon setAccessibilityHint:title.text];
            if ([favorits count]==0) {
                favoriteIcon.image = [[UIImage alloc] 
                                      initWithContentsOfFile:
                                      [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
                                       @"blankstar.png"]];

            }
            for (int i=0; i<[favorits count]; i++) {
                if ([title.text isEqualToString:[[favorits objectAtIndex:i] objectAtIndex:2]]) {
                    favoriteIcon.image = [[UIImage alloc] 
                                          initWithContentsOfFile:
                                          [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
                                           @"star.png"]];
                    break;
                }
                else {
                    favoriteIcon.image = [[UIImage alloc] 
                                          initWithContentsOfFile:
                                          [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
                                           @"blankstar.png"]];
                }
            }



            UITapGestureRecognizer *recognizer = [[[UITapGestureRecognizer alloc] 
                                                   initWithTarget:self action:@selector(AddIcone:)]autorelease
                                                  ];

            [favoriteIcon setUserInteractionEnabled:YES];
            [favoriteIcon addGestureRecognizer:recognizer];

            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
            return cell;


        }
    }



return nil;

我也尝试过:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TitleCell";
            UILabel *title;

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            cell = nil;
            if (cell == nil) {
                self.titleCell = [[[NSBundle mainBundle] loadNibNamed:@"TitleCell" owner:self options:nil]objectAtIndex:0];
                titleCell.layer.masksToBounds = YES;
                titleCell.layer.cornerRadius =0.0;
                cell = titleCell;
                self.titleCell = nil;


            }
return cell;

它像以前一样崩溃。

请求帮助我并感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

[[NSBundle mainBundle] loadNibName: owner: options:]返回一个数组。 将此行替换为:

self.titleCell = [[[NSBundle mainBundle] loadNibNamed:@"TitleCell" owner:self options:nil] objectAtIndex:0];