【问题标题】:NSArray released earlyNSArray 提前发布
【发布时间】:2011-11-02 11:06:45
【问题描述】:

当我在 UITableView 上向下滚动时,我的 iPhone 应用程序崩溃了。我将 NSZombieEnabled 设置为 YES,并发现我用来填充表格的 NSArray 正在以某种方式被释放。

#import "RootViewController.h"

@implementation RootViewController
@synthesize flashsets;

- (void)viewDidLoad
{
        //Unrelated code removed from this post
        NSString *listofsetsstring = [[NSString alloc]
                                          initWithContentsOfFile:pathtosetsdata
                                          encoding:NSUnicodeStringEncoding
                                          error:&error];
        flashsets = [listofsetsstring componentsSeparatedByString:@"\n"];
        [super viewDidLoad];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [flashsets count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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

    }

    // Configure the cell.
    NSLog(@"IndexPath.row = %i", indexPath.row);
    cell.textLabel.text = [flashsets objectAtIndex:indexPath.row]; <<<< CRASH HERE!!

    return cell;
}

@end

我在粗体线上收到message sent to deallocated instance 0x4ebae20。在我的 .h 中,我使用了 @property (nonatomic, retain) NSArray *flashsets;,我认为保留部分应该防止它被释放。

如何防止它这样做?

【问题讨论】:

    标签: iphone memory-management nsarray


    【解决方案1】:

    问题在于:

    flashsets = [listofsetsstring componentsSeparatedByString:@"\n"];
    

    改成

    flashsets = [[listofsetsstring componentsSeparatedByString:@"\n"] retain];
    

    编辑:retain in 属性仅在使用 setter 时使用,因此仅在使用以下行时才有效:

    [self setFlashsets:[listofsetsstring componentsSeparatedByString:@"\n"]];
    

    【讨论】:

    • 谢谢!以后我必须记住使用 [self variablename]。
    【解决方案2】:

    在您的 viewDidLoad 中,它应该是 self.flashsets = 这将确保使用访问器方法设置值,因此您在属性定义中指定的“保留”行为将被实现。

    【讨论】:

      【解决方案3】:
       flashsets = [listofsetsstring componentsSeparatedByString:@"\n"];//it returns autorealesed NsArray.So If you want Longer Use.you should get Owner ship from that array By Alloc or Copy Or Retain.
      
      flashsets = [[listofsetsstring componentsSeparatedByString:@"\n"] retain];
      or
      flashsets = [[listofsetsstring componentsSeparatedByString:@"\n"]copy];
      or 
      flashsets = [[NsArry alloc ] initWithArray:[listofsetsstring componentsSeparatedByString:@"\n"]];
      

      【讨论】:

        【解决方案4】:

        我不知道这是否有帮助,但您可能希望在引用 flashset 时使用 setter 和 getter 方法 - 设置变量时,属性的保留部分不适用(我不认为)直接。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-05-03
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 1970-01-01
          • 2011-04-15
          • 2011-11-24
          相关资源
          最近更新 更多