Iphone 返回tableview后崩溃

Iphone 返回tableview后崩溃,iphone,objective-c,ios,uitableview,memory-management,Iphone,Objective C,Ios,Uitableview,Memory Management,因此,我正在编写一个应用程序来读取rss提要,并在tableview中显示内容。它还允许用户播放为每个项目找到的MP3。无论如何,在我开始添加新视图之前,应用程序似乎运行良好。现在,每次我从一个视图返回并滚动一点,我都会得到“程序接收信号”SIGABRT“或类似的东西 以下是节目的大部分内容: - (IBAction)playAction:(id)sender { // Get row UIButton *senderButton = (UIButton *)sender; UITableVie

因此,我正在编写一个应用程序来读取rss提要,并在tableview中显示内容。它还允许用户播放为每个项目找到的MP3。无论如何,在我开始添加新视图之前,应用程序似乎运行良好。现在,每次我从一个视图返回并滚动一点,我都会得到“程序接收信号”SIGABRT“或类似的东西

以下是节目的大部分内容:

- (IBAction)playAction:(id)sender
{
// Get row
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = 
(UITableViewCell *) [[senderButton superview] superview];
NSInteger buttonRow = [[self.tableView 
                        indexPathForCell:buttonCell] row];

// Entry for row
RSSEntry *senderEntry = [_allEntries objectAtIndex:buttonRow];


// This is where _allEntries gets filled

- (void)requestFinished:(ASIHTTPRequest *)request {

[_queue addOperationWithBlock:^{

    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData]
                                                           options:0 error:&error];

    if (doc == nil) 
    {
        NSLog(@"Failed to parse %@", request.url);
    } 
    else 
    {

        NSMutableArray *entries = [NSMutableArray array];
        [self parseRss:doc.rootElement entries:entries]; 

        if ([_allEntries count] > 0) {

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                // Update
                int i=0;
                while (![[[_allEntries objectAtIndex:i] articleUrl] isEqualToString:[[entries objectAtIndex:i] articleUrl]]) 
                {
                    [_allEntries insertObject:[entries objectAtIndex:i] atIndex:0];
                    i++;
                }
                [self.tableView reloadData];
            }];

        }
        else
        {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                for (RSSEntry *entry in entries)
                {
                    [_allEntries addObject:entry];
                }

                NSLog(@"entries:%d", [_allEntries count]);
                [self.tableView reloadData];
            }];
        }


    }

}];



}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"View did load");

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
                                          initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
                                          target:self 
                                          action:@selector(refreshButton:)];

pauseImage = [UIImage imageNamed:@"pause_circle_small.png"];
playImage = [UIImage imageNamed:@"play_circle_small.png"];

player = nil;
isPlaying = NO;

self.title = @"Feed";
self.allEntries = [NSMutableArray array];
self.queue = [[[NSOperationQueue alloc] init] autorelease];
self.feed = [[NSString alloc] initWithString:@"http://site.org/rss/"];
[self refresh];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_allEntries count];
}

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

UILabel *mainLabel, *secondLabel;
UIButton *playBtn;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];

    mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(42.0, 5.0, 250.0, 20.0)] autorelease];
    mainLabel.tag = MAINLABEL_TAG;
    mainLabel.font = [UIFont fontWithName:@"Arial-BoldMT" size:18.0];
    mainLabel.textAlignment = UITextAlignmentLeft;
    mainLabel.textColor = [UIColor blackColor];
    mainLabel.highlightedTextColor = [UIColor whiteColor];
    [cell.contentView addSubview:mainLabel];

    secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(42.0, 27.0, 250.0, 15.0)] autorelease];
    secondLabel.tag = SECONDLABEL_TAG;
    secondLabel.font = [UIFont fontWithName:@"ArialMT" size:14.0];
    secondLabel.textAlignment = UITextAlignmentLeft;
    secondLabel.textColor = [UIColor colorWithRed:222.0/255.0 green:95.0/255.0 
                                             blue:199.0/255.0 alpha:1.0];
    secondLabel.highlightedTextColor = [UIColor whiteColor];
    [cell.contentView addSubview:secondLabel];

    playBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    playBtn.tag = PLAYBTN_TAG;
    playBtn.frame = CGRectMake(2.0, 6.0, playImage.size.width, playImage.size.height);
    [playBtn setBackgroundImage:playImage forState:UIControlStateNormal];
    //[playBtn setBackgroundImage:playImage forState:UIControlStateHighlighted];

    [playBtn addTarget:self action:@selector(playTapped:) 
      forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:playBtn];
}
else 
{
    mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
    secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
    playBtn = (UIButton *)[cell.contentView viewWithTag:PLAYBTN_TAG];
}

// Alternate bg color
if (indexPath.row%2 == 0) {
    UIColor *altColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0
                                         blue:230.0/255.0 alpha:1];
    mainLabel.backgroundColor = altColor;
    secondLabel.backgroundColor = altColor;
}
else
{
    UIColor *altColor = [UIColor colorWithRed:255.0 green:255.0
                                         blue:255.0 alpha:1];
    mainLabel.backgroundColor = altColor;
    secondLabel.backgroundColor = altColor;
}

RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row];
NSLog(@"Entry: %@", entry);

// Manage play button
if (entry == currEntry)
{
    if(isPlaying)
    {
        [playBtn setBackgroundImage:pauseImage forState:UIControlStateNormal];
    }
    else
    {
        [playBtn  setBackgroundImage:playImage forState:UIControlStateNormal];
    }
}
else
    [playBtn setBackgroundImage:playImage forState:UIControlStateNormal];

mainLabel.text = entry.articleTitle;
secondLabel.text = entry.articleArtist;

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.

DetailView *detailViewController = [[DetailView alloc] initWithNibName:@"DetailedView" bundle:[NSBundle mainBundle]];

RSSEntry *entry = [_allEntries objectAtIndex:[indexPath row]];



[self.navigationController pushViewController:detailViewController animated:YES];

detailViewController.songTitle.text = entry.articleTitle;
detailViewController.artistName.text = entry.articleArtist;

[entry release];

[detailViewController release];

}

- (void)dealloc
{
[player release];
player = nil;
[_queue release];
_queue = nil;
[_feed release];
_feed = nil;
[_allEntries release];
_allEntries = nil;

[super dealloc];
}

@end

如果没有线路,你会在哪里崩溃,这很难说,但很可能你访问了某个对象,什么是解除锁定的

很可能是这里

self.feed = [[NSString alloc] initWithString:@"http://site.org/rss/music"];
[self.feed release];

您可以立即释放对象,但如果不知道您是否保留了属性,就很难说出来。请不要释放任何@synthesis变量。您应该只使用dealloc方法发布它,这是一种猜测,但您不会保留在
viewDidLoad
中获得的图像:

pauseImage = [UIImage imageNamed:@"pause_circle_small.png"];
playImage = [UIImage imageNamed:@"play_circle_small.png"];

使用retaining属性和点语法或发送每个a
retain

请不要释放self.feed,并且在卸载或解除锁定视图时,放置委托nil意味着

tableview.delegate=nil


这是最主要的检查,在这之后,我想你不要取消tableview的代理。

AHAA!!!我把我的RSSEntry设置为自动释放,然后再将它们放入allEntries数组。当我改变观点时,他们正在被解雇。不要那样做。谢谢大家的帮助。那太简单了,我现在觉得自己很笨

不要在
视图didload:
中释放iVar,应改为在
delloc
方法中释放iVar。删除此项:
[self.feed release]。我不能肯定它能解决你的问题因此,当它在
mainlab.text=entry.articletTitle
上崩溃时,它实际上指向了一个错误的地址。我只是不知道我的RSSEntry对象是如何改变的。你确定坏地址在
条目上吗
不是
mainLabel
?根据@Eiko的回答,我发现你的
pauseImage
&
playImage
是iVar!它们将在退出该方法时自动释放,只需遵循@Eiko的建议即可@是的,我修好了。由于某种原因,我的
articletTitle
articleArtist
崩溃时为零。它们根本不应该改变。有时它会在“mainlab.text=entry.articletTitle”行崩溃;但是很难看到RSSEntry会被解除锁定,当视图切换时,我不会解除任何锁定。无论如何,我不这么认为……这不是一个好的模式,但如果feed是一个“保留”属性,那么在这里很可能还可以。(我们不知道,但应该期待)啊,我没有在那里使用保留,所以是的,我修复了它。仍在崩溃。如果它在mainLabel.text=。。。行,在该行之前设置一个断点,并检查标签和条目。您的意思是在-dealoc方法中,我应该释放委托?我不知道。但是dealloc直到程序的最后才被调用。很好的调用,但是我修复了它,它仍然是相同的情况。每当我离开tableview并返回时,它就会崩溃。我确信您释放了一些不应该释放的变量。尝试对所有变量使用NSAutoreleasePool。