简体   繁体   中英

releasing thread1 exc_bad_access

I am new at IOS programing and I have program that works fine, but I found out that it has memory leek, so I start releasing object.

When I now start the program it give me an error:

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

and :

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

I tried to debug it and I found out that program crashed at creating tableView. It create whole first section, but in the second row in second section it crashed in the returning line.

here is my code of creating table:

- (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;

I also tried just putting :

- (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;

and it crash as before.

pleas help me out and thank for your help.

[[NSBundle mainBundle] loadNibName: owner: options:] returns an array. Replace this line with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM