“EXC_BAD_ACCESS”,“ - [CFString release]:在表格中滚动时发送到解除分配的实例的消息”

时间:2011-02-08 07:39:06

标签: iphone

滚动UITableview时出现以下错误。 EXC_BAD_ACCESS - [CFString retain]:发送到解除分配的实例的消息

我怎样才能找到解除分配的实例...? 这是我的noOfRowsinsection代码

for(int i=0;i<size;i++)
{
    NSString *CellIdentifier1;
    if(universalApp==2)
    {
        NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
    CellIdentifier1 = @"CustomThumbImageTableCell_iphone";
    cell = [[[CustomThumbImageTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];

        //NSLog(@">>>>> Creating image >>>>>>>>");
    //cell.thumbImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(4, 4, 83, 101)];
    cell.thumbImageView = [[[CustomImageView alloc] initWithFrame:CGRectMake(4, 4, 83, 101)] autorelease];

    [imgViewArray addObject:cell.thumbImageView];

    [cell.thumbImageView release];  
        [pool release];

    }

这是我的cellforRow代码

enter code hereif(universalApp==2)
{
    NSLog(@"iphone cell>>>>>>>>>>>>>>>>>>>>>>");

    CustomThumbImageTableCell *cell = nil;


    @try
    {
        static NSString *CellIdentifier = @"CustomThumbImageTableCell_iphone";
        int currentRow = [indexPath row] ;//+ skippedItems;
        //NSLog(@">>>>>>>>>>>>Current Table row = %d, %d, %d", loadedCellArray.count, currentRow, [Table numberOfRowsInSection:0]);


        /*if(loadedCellArray.count > currentRow + 1 && [loadedCellArray objectAtIndex:(currentRow)] != nil)
         {
         NSLog(@"Image updated = %d", ( currentRow));

         cell = (CustomThumbImageTableCell_iphone *) [loadedCellArray objectAtIndex:(currentRow )];

         }
         else
         {*/

        /*if(loadedCellArray.count > currentRow + 1 )// && [loadedCellArray objectAtIndex:(currentRow)] != nil)
         // if( [loadedCellArray objectAtIndex:(currentRow)] != nil)
         {
         //NSLog(@"Image updated = %d", ( currentRow));

         cell = (CustomThumbImageTableCell_iphone *) [loadedCellArray objectAtIndex:([indexPath row]  )];
         if(!isScrollingUp) 
         {
         //scrollCount++;
         [loadedCellArray removeLastObject];
         }
         isScrollingUp = YES;

         }
         else*/
        {

            static NSString *CellIdentifier = @"CustomThumbImageTableCell_iphone";
            static NSString *CellIdentifier1 = @"CustomThumbImageTableCell_iphone";
            //UITableViewCell *cell;

            if(cell==nil)
            {
                cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                cell = [[[CustomThumbImageTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            }
            else
            {
                cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
                cell = [[[CustomThumbImageTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];

            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            NSLog(@">>>>>>>>>..... %d, %d, %@", imgViewArray.count, indexPath.row, (((int)imgViewArray.count- 1) < (int)indexPath.row) ? @"YES" : @"NO");
            /*if(((int)imgViewArray.count- 1) < (int)indexPath.row)
            {
                //  NSLog(@">>>>> Creating image >>>>>>>>");
                cell.thumbImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(4, 4, 83, 101)];

                [imgViewArray addObject:cell.thumbImageView];
            }
            else {
                cell.thumbImageView = (CustomImageView *) [imgViewArray objectAtIndex:indexPath.row];
            }*/
            //NSLog(@"img array = %@,%d",imgViewArray,imgViewArray.count);

            cell.thumbImageView =[imgViewArray objectAtIndex:indexPath.row];
            cell.thumbImageView.contentMode = UIViewContentModeScaleAspectFit;
            [cell.contentView addSubview:cell.thumbImageView];

            cell.thumbImageView.index = (int)indexPath.row;

            @try
            {

                {
                    BOOL isFound = NO;
                    do
                    {   
                        @try {
                            NSArray *array1 = [contentString componentsSeparatedByString:@"@@#"];
                            NSArray *array2 = [[array1 objectAtIndex:currentRow ] componentsSeparatedByString:@"##"];           
                            NSString *str = [NSString stringWithFormat:@"%@", [array2 objectAtIndex:0]];
                            str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

                            UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(104, 6, 141, 96)];
                            name.textAlignment = UITextAlignmentCenter;
                            name.font = [UIFont fontWithName:@"Arial" size:17.0];
                            name.numberOfLines = 3;
                            name.backgroundColor = [UIColor clearColor];
                            name.textColor = [UIColor whiteColor];
                            cell.catalogName = name;
                            [cell.catalogName setText:[NSString stringWithFormat:@"%@", str]];
                            [cell.contentView addSubview:cell.catalogName];
                            //[name release];

                            name = [[UILabel alloc] initWithFrame:CGRectMake(107, 89, 193, 21)];//107,89,193
                            name.textAlignment = UITextAlignmentCenter;
                            name.font = [UIFont fontWithName:@"Arial" size:13.0];
                            name.textColor = [UIColor whiteColor];
                            name.numberOfLines = 3;
                            name.backgroundColor = [UIColor clearColor];
                            name.alpha = 0.26;
                                cell.pageNo = name;

                                [cell.contentView addSubview:cell.pageNo];
                            //[name release];



                            if((searchId != 2 && (isLineNameSearchEnabled == 0)) || searchInCatalogFlag == 1)
                                //cell  Page No//pageNoLabel
                                //NSLog(@"cell for row.......");
                                [cell.pageNo setText:[NSString stringWithFormat:@"Page No: %@", [array2 objectAtIndex:(array2.count - 2)]]];


                            {

                            }

                            NSLog(@" thumb image = %@",[NSString stringWithFormat:@"%@%@", baseURL,  [array2 objectAtIndex:1]]);

                            //[cell.thumbImageView setImageURL:@"http://www.zoomcatalog.com/sites/default/files/catalogs/27705_Abex2010/images/thumbnails/Thumb-1.jpg"];
                            [cell.thumbImageView setImageURL:[NSString stringWithFormat:@"%@%@", baseURL,  [array2 objectAtIndex:1]]];
                            [NSThread detachNewThreadSelector:@selector(initThread) toTarget:cell.thumbImageView withObject:nil];
                            [cell.thumbImageView setPageNo:((searchId == 2 || (isLineNameSearchEnabled && searchInCatalogFlag == 0)) ? 0 : (int)currentRow)];


                            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(264, 33, 36, 34)];
                            img.contentMode = UIViewContentModeScaleToFill;
                            [img setImage:cellArrow];
                            [cell.contentView addSubview:img];

                            [img release];

                            [cell.catalogName release];
                            //>>>>      [cell.pageNoLabel release];
                            [cell.thumbImageView release]; 
                            isFound = YES;
                        }
                        @catch (NSException * e) {
                            //skippedItems++;
                            currentRow = [indexPath row] + 1;
                        }

                    }
                    while (!isFound);




                    /*if(isScrollingUp)
                     {
                     //scrollCount++;
                     [loadedCellArray removeLastObject];
                     }
                     isScrollingUp = NO;
                     currentRow =indexPath.row;     
                     // cell = (CustomThumbImageTableCell_iphone *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                     if (cell == nil)
                     {
                     //cell = (CustomThumbImageTableCell_iphone *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

                     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomThumbImageTableCell_iphone" owner:self options:nil];
                     //cell = (CustomThumbImageTableCell_iphone *) [loadedCellArray objectAtIndex:(currentRow )];
                     cell = self.cell;
                     self.cell = nil;

                     }  
                     NSLog(@"Image loaded from source= %d,", ( currentRow));
                     //if(loadedCellArray.count > currentRow + 1)
                     // NSLog(@"Image loaded from source cell array ref= %@", [loadedCellArray objectAtIndex:(currentRow)]);

                     /*if(currentRow == ([Table numberOfRowsInSection:0] - 1))
                     {
                     cell.catalogName.text = @"50 More";
                     NSLog(@">>>>>>>>>>>>>>>>>>>>> End cell....");
                     return cell;
                     }
                     /////
                     // if(true)
                     // return cell;
                     @try
                     {

                     {
                     BOOL isFound = NO;
                     do
                     {  
                     @try {
                     NSArray *array1 = [contentString componentsSeparatedByString:@"@@#"];
                     NSArray *array2 = [[array1 objectAtIndex:currentRow] componentsSeparatedByString:@"##"];           
                     NSString *str = [NSString stringWithFormat:@"%@", [array2 objectAtIndex:0]];
                     str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                     // NSLog(@"&&&&&&&&&&&&&&&&& %@", str);
                     [cell.catalogName setText:[NSString stringWithFormat:@"%@", str]];
                     if((searchId != 2 && (isLineNameSearchEnabled == 0)) || searchInCatalogFlag == 1)
                     [cell.pageNo setText:[NSString stringWithFormat:@"Page No: %@", [array2 objectAtIndex:(array2.count - 2)]]];

                     // NSLog(@"Background color has been set....%d", i);
                     //NSLog(@")
                     //NSLog(@"Catalog %d = %@", currentRow, [array1 objectAtIndex:currentRow]);
                     // NSLog(@"Image URL = %@", [NSString stringWithFormat:@"%@%@", baseURL,  [array2 objectAtIndex:1]]);

                     [cell.thumbImageView setImageURL:[NSString stringWithFormat:@"%@%@", baseURL,  [array2 objectAtIndex:1]]];
                     //[cell.thumbImageView setContentString:contentString];
                     //if(searchId == 2)
                     // [cell.thumbImageView setContentString:[NSString stringWithFormat:@"%@", [array2 objectAtIndex:(array2.count - (isLineNameSearch ? 2 :  2))]]];

                     [cell.thumbImageView performSelectorOnMainThread:@selector(initThread) withObject:nil waitUntilDone:NO];
                     [cell.thumbImageView setPageNo:(searchId == 2 ? @"0" : [indexPath row])];

                     isFound = YES;
                     }
                     @catch (NSException * e) {
                     //skippedItems++;
                     currentRow = [indexPath row] + 1;
                     }
                     */ 
                    //  }
                    //  while(!isFound);// && currentRow < [Table numberOfRowsInSection:0]);

                    //[imgViewArray addObject:cell.thumbImageView];



                    //  [imgViewArray retain];



                    //[label setTextColor:[UIColor blackColor]];
                    //[label setTextAlignment:UITextAlignmentCenter];
                    /*          UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(7 + ((i % 3) * (96 + 8)) , 9 + ((i / 3) * (140 + 10)), 98, 142)];
                     [label setBackgroundColor:[UIColor grayColor]];
                     [scrollView addSubview:label];

                     [imgViewArray addObject:imgView];
                     [scrollView addSubview:imgView];
                     if(i % 3 == 0)
                     {
                     [scrollView setContentSize:CGSizeMake(scrollView.contentSize.width, scrollView.contentSize.height + (140 + 10))];
                     }*/
                }
                //[imgViewArray retain];
                //  [cellArray retain];
            }

2 个答案:

答案 0 :(得分:5)

当您遇到内存管理问题时,您可以执行以下操作:

  1. 重新阅读Cocoa memory management rules并确保您关注它们。
  2. 运行static analyser。这通常会占用您忽略内存管理规则的地方。
  3. 由于您似乎已启用NSZombies,请为[_NSZombie release]插入断点。这应该有望打破导致问题的界限。
  4. 编辑:

    根据您的代码,以下行可能是罪魁祸首。

    [cell.thumbImageView release]; // NOT GOOD
    

    同样在你的其他代码中:

    [cell.thumbImageView release]; // NOT GOOD
    [cell.catalogName release]; // NOT GOOD
    

    您将自动释放的对象设置为单元格的thumbImageViewcatalogName属性,但您仍在尝试释放它。这不是Cocoa内存管理的工作原理。您没有拥有 cell.thumbImageViewcell.catalogName,因此您不应该发布它们。

答案 1 :(得分:2)

当您尝试访问已释放的对象时,会发生此问题。在您访问它之前,可能是您的字符串已发布。