0

我面临着一个简单而烦人的内存释放问题和向已释放对象发送消息的问题。

我正在尝试从网页获取 Rss 提要。我在第一阶段分两个阶段开发了小程序,因为我对 iPhone 开发还不熟悉,所以我尝试从网页中获取 Rss 提要的文本部分,成功且没有问题。在第 2 阶段,我尝试获取图像的链接并在每个 UITableViewCell 和之后的详细视图上呈现它们。

我有一个加载 rss 的类,负责解析和返回我返回的可变数组[array copy]。一切正常,即使加载需要一些时间。当我运行应用程序并尝试滚动表格视图时,我遇到崩溃和 NSZombie 消息:

objc [37372]:FREED(id):消息保留发送到释放对象=0x3930cd0

当我尝试在不滚动的情况下加载 detailViewController 时也会发生这种情况,我发布了一些 tableViewController 以查看问题所在。

- (void)viewDidLoad {
    [super viewDidLoad];
    post = [[Posts alloc]init];
    //self.tableView.rowHeight = 160.0;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
    NSLog(@"parser is about to init");
    parser = [[XmlParser alloc] init];
    array = [parser initWithURL:@"http://backend.deviantart.com/rss.xml?q=boost%3Apopular+meta%3Aall+max_age%3A8h&type=deviation&offset=0"];
    //array = [parser initWithURL:@"http://www.enet.gr/rss?i=news.el.categories&c=politikh"];
    NSLog(@"posts has %d items",[array count]);
    [parser release];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        post = [array objectAtIndex:indexPath.row];
        // Set up the cell...
        [cell.textLabel text:[post itemTitle]];
        [cell.detailTextLabel setText:[post itemBody]];
        [cell.imageView setImage:[self.post itemthumbnail]];

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
     FirstDetailedViewController *anotherViewController = [[[FirstDetailedViewController alloc] initWithNibName:@"FirstDetailedViewController" bundle:nil]autorelease];
    NSLog(@"posts count : %d", [array count]);
    post = [array objectAtIndex:indexPath.row];
    anotherViewController.currpost = [post retain];
    [self.navigationController pushViewController:anotherViewController animated:YES];
}

后冲突是一些 NSMutableStrings 和 UIImages,它们都在它们自己的 init 中分配和初始化,并在它们自己的 dealloc 方法中释放。当我尝试在不滚动的情况下点击单元格时,我也遇到了问题,但那是明天的问题。

4

3 回答 3

1
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    post = [array objectAtIndex:indexPath.row];
    // Set up the cell...
    [cell.textLabel text:[post itemTitle]];
    [cell.detailTextLabel setText:[post itemBody]];
    [cell.imageView setImage:[self.post itemthumbnail]];

}
return cell;}

您不想在代码中的此位置配置每个单元格以显示。您的代码应如下所示:

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    post = [array objectAtIndex:indexPath.row];
    // Set up the cell...
    [cell.textLabel text:[post itemTitle]];
    [cell.detailTextLabel setText:[post itemBody]];
    [cell.imageView setImage:[self.post itemthumbnail]];

   return cell;
}
于 2011-02-15T01:40:05.317 回答
1

解决了!简单的初始化问题

以前的 :

    -(id)init
{   
    [super init];
    channelTitle = [[NSMutableString alloc]init];
    channelLink = [[NSMutableString alloc]init];
    channelDescription = [[NSMutableString alloc]init];
    channelPubDate = [[NSMutableString alloc]init];
    itemTitle = [[NSMutableString alloc]init];
    itemBody =  [[NSMutableString alloc]init];
    itemPubDate = [[NSMutableString alloc]init];
    itemLink = [[NSMutableString alloc]init];
    itemthumbnail = [[UIImage alloc]init];
    itemthumbnailLink = [[NSString alloc]init];
    itemImage = [[UIImage alloc]init];
    itemImageLink = [[NSString alloc]init];
    return self;
}

现在 :

-(id)init
{   
    if (self==[super init]){
    channelTitle = [[NSMutableString alloc]init];
    channelLink = [[NSMutableString alloc]init];
    channelDescription = [[NSMutableString alloc]init];
    channelPubDate = [[NSMutableString alloc]init];
    itemTitle = [[NSMutableString alloc]init];
    itemBody =  [[NSMutableString alloc]init];
    itemPubDate = [[NSMutableString alloc]init];
    itemLink = [[NSMutableString alloc]init];
    itemthumbnail = [[UIImage alloc]init];
    itemthumbnailLink = [[NSString alloc]init];
    itemImage = [[UIImage alloc]init];
    itemImageLink = [[NSString alloc]init];
    }
    return self;
}

实际图像从未分配!现在它是!:D

于 2011-02-15T12:30:10.883 回答
0

很可能是您的数据源过早自动发布的问题。尝试保留array对象或在您的viewDidLoad方法中为其声明(保留)属性:

NSArray *tmpArray = [parser initWithURL:@"http://backend.deviantart.com/rss.xml?q=boost%3Apopular+meta%3Aall+max_age%3A8h&type=deviation&offset=0"];
self.array = tmpArray;

希望有帮助。罗格

于 2011-02-15T01:52:18.040 回答