【问题标题】:NSZombieEnabled prevents my app from crashingNSZombieEnabled 防止我的​​应用程序崩溃
【发布时间】:2012-05-21 21:24:35
【问题描述】:

所以我一直在像疯子一样在 Instruments 中使用 NSZombiesEnabled 和 NSZombies 进行调试。但是,当使用僵尸运行应用程序时,它似乎解决了我的问题。当我在仪器中运行没有 NSZombiesEnabled 或 NSZombies 的应用程序时,它会崩溃。关于如何处理这个问题的任何想法?

所以问题是我发布了两次,但似乎找不到我在哪里做这件事。打开 NSZombieEnabled 将无济于事,因为程序运行良好,但没有告诉我我在哪里过度释放。

所以我想我知道它在哪里崩溃了,我正在创建这个 globalArray Singleton 类:

extern NSString * const kClearDataSource;

@interface AHImageDataSource : NSObject
+ (AHImageDataSource *)sharedDataSource;
- (void) clearDataSource;
- (void) addObject:(id) object;
- (void) addObject:(id)object atIndex:(int) index;
- (int) count;
- (id) objectAtIndex:(int) index;
@end



NSString * const kClearDataSource = @"clearDataSource";

@interface AHImageDataSource()
{
    NSMutableArray * imageDataSource_;
}

@property (nonatomic, retain) NSMutableArray * imageDataSource_;

@end

@implementation AHImageDataSource
@synthesize imageDataSource_;

+ (AHImageDataSource *)sharedDataSource {
    static AHImageDataSource *_sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] init];
    });

    return _sharedClient;
}


- (id)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    NSMutableArray * temp = [[NSMutableArray alloc] initWithCapacity:200];
    self.imageDataSource_  = temp;
    [temp release];


    return self;
}

-(void) clearDataSource
{
    if ([self.imageDataSource_ count] > 0){
        [self.imageDataSource_ removeAllObjects];
    }
}

- (void) addObject:(id) object
{
    [self.imageDataSource_ addObject:object];
}

- (void) addObject:(id)object atIndex:(int) index
{
    [self.imageDataSource_ insertObject:object atIndex:index];
}

- (int) count
{
    return [self.imageDataSource_ count];
}

- (id) objectAtIndex:(int) index
{
    if (index >= 0 && index < [self.imageDataSource_ count]){
        return [self.imageDataSource_ objectAtIndex:index];
    } 

    return nil;
}

- (void) dealloc
{
    [super dealloc];
    [imageDataSource_ release];
}

@end

在代码的某一点,我试图删除数组中的所有对象,然后添加一些东西。当这种情况发生时,崩溃就发生了。

这部分代码第二次执行就崩溃了:

 NSArray *arr = [response valueForKey:@"data"];
                 if ([arr count] > 0){
                     [[AHImageDataSource sharedDataSource] clearDataSource];
                 }

                for (NSDictionary * data in arr){
                     AHInstagramImageData * imgData = [[AHInstagramImageData alloc] initWithData:data];
                     [[AHImageDataSource sharedDataSource] addObject:imgData];
                     [imgData release];
                 }

【问题讨论】:

标签: iphone objective-c ios ipad


【解决方案1】:

你绝对不应该在你的-dealloc 方法中使用[super dealloc] first。它必须最后来。

【讨论】:

  • 另外,你应该使用ARC,在这种情况下,你根本不需要[super dealloc]语句。
  • 首先拥有 [super dealloc] 是导致我遇到问题的原因。
【解决方案2】:

Go go 产品 -> 分析。显示的消息将为您提供解决方案或想法。

【讨论】:

    【解决方案3】:

    当向已释放的对象发送消息时,您的应用会崩溃。 NSZombiesEnabled 可以防止您的应用程序崩溃,因为它会保留所有已释放的对象(因此会泄漏所有内容)。当释放的对象被发送消息(这通常会使您的应用程序崩溃)时,它将在控制台中打印一条消息。影响“发送到已释放对象'foo'的消息'bar'”(或类似的东西)的影响。它实际上不会暂停您的应用程序的执行。

    当您知道您的应用通常会崩溃时,请检查控制台日志以获取与上述类似的消息。

    【讨论】:

    • 所以你是说开启 NSZombiesEnabled?
    • 是的,要对代码进行故障排除,您可以将其打开,然后当应用程序没有在正常情况下崩溃时,在控制台中查看发送到已释放对象的消息。
    • 当 NSZombieEnabled 存在时,没有任何内容输出到控制台
    • 我记得,当释放的实例是消息时,Zombies 工具确实会自动停止您的应用程序。
    猜你喜欢
    • 1970-01-01
    • 2011-08-26
    • 2013-05-30
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2013-03-08
    • 2011-10-13
    相关资源
    最近更新 更多