简体   繁体   English

在这种情况下,为什么我会收到一条消息发送到已释放实例错误?

[英]Why am I getting a message sent to deallocated instance error in this case?

I am trying to sort a NSMutableArray based on an object it contains.我正在尝试根据它包含的 object 对 NSMutableArray 进行排序。 I am getting an error on this line in the code segment below:我在下面的代码段中的这一行出现错误:

InboxItem * ptrInboxItem =  [sortedInboxFaxItems objectAtIndex:[indexPath row]];

#import <UIKit/UIKit.h>
@class InboxItem;


@interface InboxTableViewController : UITableViewController<NSXMLParserDelegate> {
    NSMutableArray *inboxFaxItems;
    NSArray * sortedInboxFaxItems;
    InboxItem *_inboxItem;

    NSMutableData *xmlData;
    NSURLConnection *connectionInprogress;
    NSMutableString *inboxFaxesString;
    UIActivityIndicatorView *activityIndicator;
}

@property(nonatomic,retain) InboxItem * inboxItem;

-(void) loadInbox;

@end

- (void) connectionDidFinishLoading:(NSURLConnection *)connection{
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:xmlData];
    [parser setDelegate:self];
    [parser parse];
    [parser release];

    //lets sort by messageID
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"messageID" ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    sortedInboxFaxItems = [inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors]; 

    [[self tableView] reloadData];
    activityIndicator.stopAnimating;

    [connectionInprogress release];
    connectionInprogress = nil;

    [xmlData release];
    xmlData = nil;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"InboxFaxItem";

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

    //I AM GETTING ERROR HERE
    InboxItem * ptrInboxItem =  [sortedInboxFaxItems objectAtIndex:[indexPath row]];

    [[cell textLabel]setText: ptrInboxItem.datetime];
    cell.imageView.image = [UIImage imageNamed:@"document.png"];
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

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

    MyManager *sharedManager = [MyManager sharedManager];
    InboxItem * ptrInboxItem =  [sortedInboxFaxItems objectAtIndex:[indexPath row]];

    sharedManager.pages = ptrInboxItem.pages;
    sharedManager.from =ptrInboxItem.from;

    FaxViewController *faxViewController = [[FaxViewController alloc] initWithNibName:@"FaxViewController" bundle:nil];
    faxViewController.messageid=ptrInboxItem.messageID;
    faxViewController.navigationItem.title=@"View Fax";
    [self.navigationController pushViewController:faxViewController animated:YES];
    [faxViewController release];
}

It's because of this line:这是因为这条线:

sortedInboxFaxItems = [inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors];

You're assigning an object you don't own to an instance variable.您正在将不属于您的 object 分配给实例变量。 Later, when you try to access it, that object has been deallocated, so your instance variable now points to garbage.后来,当您尝试访问它时,该 object 已被释放,因此您的实例变量现在指向垃圾。

You should change that line to this:您应该将该行更改为:

[sortedInboxFaxItems release]; // Release any previous value
sortedInboxFaxItems = [[inboxFaxItems sortedArrayUsingDescriptors:sortDescriptors] retain];

All better.一切都好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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