使用带有Xcode 4.2的Objective-C ARC时,如何防止对象被释放?

Ele*_*zar 6 xcode memory-management objective-c ios automatic-ref-counting

ETA:通过分析应用程序获得更多信息,查看底部.

我有一个iPhone应用程序,我刚刚转换为使用ARC,现在我因为僵尸对象而得到了几个错误.在我切换之前,我手动保留它们,一切都很好.我无法弄清楚为什么ARC没有保留它们.对象声明为强属性,并使用点表示法引用.这种情况发生在几个地方,所以我认为我必须对某处的ARC /内存管理有一个基本的误解.

这是一个特别令人沮丧的例子.我有一个3个对象的NSMutableArray.每个对象都有一个属性,它也是一个NSMutableArray,在这种情况下总是有一个对象.最后,该对象具有释放的属性.它令人沮丧的原因是它只发生在原始数组中的第3个对象.前两个对象总是很好.对于我来说,当以相同方式创建和使用的相似对象的相同属性不是这样时,如何释放一个对象的属性是没有意义的.

该数组作为属性存储在UITableViewController上:

@interface GenSchedController : UITableViewController <SectionHeaderViewDelegate>

@property (nonatomic, strong) NSArray *classes;

@end

@implementation GenSchedController

@synthesize classes;
Run Code Online (Sandbox Code Playgroud)

存储在classes数组中的对象定义为:

@interface SchoolClass : NSObject <NSCopying, NSCoding>

@property (nonatomic, strong) NSMutableArray *schedules;

@end

@implementation SchoolClass

@synthesize schedules;
Run Code Online (Sandbox Code Playgroud)

存储在schedules数组中的对象定义为:

@interface Schedule : NSObject <NSCopying, NSCoding>

@property (nonatomic, strong) NSMutableArray *daysOfWeek;

@implementation Schedule

@synthesize daysOfWeek;
Run Code Online (Sandbox Code Playgroud)

daysOfWeek是什么被释放.它只包含几个NSStrings.

我可以看到,在那期间viewDidLoad所有物体都很好,没有僵尸.但是,当我点击其中一个表格单元格并在第一行设置断点时tableView:didSelectRowAtIndexPath:,它已经被释放.抛出错误的特定行是在@synthesize daysOfWeek;下面的第3个"for"循环之后调用的:

for (SchoolClass *currentClass in self.classes) {
    for (Schedule *currentSched in currentClass.schedules) {
        for (NSString *day in currentSched.daysOfWeek)
Run Code Online (Sandbox Code Playgroud)

但是,再次,这只发生在最后一个SchoolClass的最后一个时间表上.

任何人都可以指出我正确的方向让我的应用程序与ARC正常工作?

根据要求,这里有更多信息.首先,抛出异常时的堆栈跟踪:

#0  0x01356657 in ___forwarding___ ()
#1  0x01356522 in __forwarding_prep_0___ ()
#2  0x00002613 in __arclite_objc_retainAutoreleaseReturnValue (obj=0x4e28b80) at /SourceCache/arclite_host/arclite-4/source/arclite.m:231
#3  0x0000d2fc in -[Schedule daysOfWeek] (self=0x4e28680, _cmd=0x220d6) at /Users/Jesse/Documents/Xcode/Class Test/Schedule.m:18
#4  0x0001c161 in -[SchedulesViewController doesScheduleOverlap:schedule2:withBufferMinutes:] (self=0x692b210, _cmd=0x22d58, schedule1=0x4e28680, schedule2=0x4e27f10, buffer=15) at /Users/Jesse/Documents/Xcode/Class Test/Classes/SchedulesViewController.m:27
#5  0x0001c776 in -[SchedulesViewController doesScheduleOverlap:schedule2:] (self=0x692b210, _cmd=0x22d9b, schedule1=0x4e28680, schedule2=0x4e27f10) at /Users/Jesse/Documents/Xcode/Class Test/Classes/SchedulesViewController.m:53
#6  0x0001cf8c in -[SchedulesViewController getAllowedSchedules] (self=0x692b210, _cmd=0x22dca) at /Users/Jesse/Documents/Xcode/Class Test/Classes/SchedulesViewController.m:78
#7  0x0001d764 in -[SchedulesViewController viewDidLoad] (self=0x692b210, _cmd=0x97cfd0) at /Users/Jesse/Documents/Xcode/Class Test/Classes/SchedulesViewController.m:121
#8  0x00620089 in -[UIViewController view] ()
#9  0x0061e482 in -[UIViewController contentScrollView] ()
#10 0x0062ef25 in -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] ()
#11 0x0062d555 in -[UINavigationController _layoutViewController:] ()
#12 0x0062e7aa in -[UINavigationController _startTransition:fromViewController:toViewController:] ()
#13 0x0062932a in -[UINavigationController _startDeferredTransitionIfNeeded] ()
#14 0x00630562 in -[UINavigationController pushViewController:transition:forceImmediate:] ()
#15 0x006291c4 in -[UINavigationController pushViewController:animated:] ()
#16 0x000115d5 in -[GenSchedController tableView:didSelectRowAtIndexPath:] (self=0x4c57b00, _cmd=0x9ac1b0, tableView=0x511c800, indexPath=0x4e2cb40) at /Users/Jesse/Documents/Xcode/Class Test/Classes/GenSchedController.m:234
#17 0x005e7b68 in -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] ()
#18 0x005ddb05 in -[UITableView _userSelectRowAtPendingSelectionIndexPath:] ()
#19 0x002ef79e in __NSFireDelayedPerform ()
#20 0x013c68c3 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ ()
#21 0x013c7e74 in __CFRunLoopDoTimer ()
#22 0x013242c9 in __CFRunLoopRun ()
#23 0x01323840 in CFRunLoopRunSpecific ()
#24 0x01323761 in CFRunLoopRunInMode ()
#25 0x01aa71c4 in GSEventRunModal ()
#26 0x01aa7289 in GSEventRun ()
#27 0x0057ec93 in UIApplicationMain ()
#28 0x0000278d in main (argc=1, argv=0xbffff5fc) at /Users/Jesse/Documents/Xcode/Class Test/main.m:16
Run Code Online (Sandbox Code Playgroud)

确切的例外是 Class Test[82054:b903] *** -[__NSArrayM respondsToSelector:]: message sent to deallocated instance 0x4e28b80

这里是创建所有内容的代码,从磁盘加载:

NSString *documentsDirectory = [FileManager getPrivateDocsDir];

NSError *error;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

// Create SchoolClass for each file
NSMutableArray *classesTemp = [NSMutableArray arrayWithCapacity:files.count];
for (NSString *file in files) {
    if ([file.pathExtension compare:@"sched" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];

        NSData *codedData = [[NSData alloc] initWithContentsOfFile:fullPath];
        if (codedData == nil) break;

        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:codedData];
        SchoolClass *class = [unarchiver decodeObjectForKey:@"class"];    
        [unarchiver finishDecoding];

        class.filePath = fullPath;

        [classesTemp addObject:class];
    }
}

self.classes = classesTemp;
Run Code Online (Sandbox Code Playgroud)

initWithCoder:方法非常简单.第一个为SchoolClass:

- (id)initWithCoder:(NSCoder *)decoder {
    self.name = [decoder decodeObjectForKey:@"name"];
    self.description = [decoder decodeObjectForKey:@"description"];
    self.schedules = [decoder decodeObjectForKey:@"schedules"];

    return self;
}
Run Code Online (Sandbox Code Playgroud)

对于时间表:

- (id)initWithCoder:(NSCoder *)decoder {
    self.classID = [decoder decodeObjectForKey:@"id"];
    self.startTime = [decoder decodeObjectForKey:@"startTime"];
    self.endTime = [decoder decodeObjectForKey:@"endTime"];
    self.daysOfWeek = [decoder decodeObjectForKey:@"daysOfWeek"];

    return self;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用Zombies模板在应用程序上运行配置文件,并将过度释放的对象与数组中的其他一个对象进行比较.我可以看到,在线上for (NSString *day in currentSched.daysOfWeek),它进入了daysOfWeek getter,它做了一个retain autorelease.然后在它从getter返回后,它执行另一个retain(可能是在处理循环时保持所有权),然后是a release.对于健康对象,问题对象的所有这些都是相同的.不同之处在于release,问题对象之后立即调用releaseAGAIN.这实际上不会立即引起问题,因为自动释放池还没有耗尽,但是一旦它完成,保留计数就会降到0,然后当我下次尝试访问它时,它就是一个僵尸.

我无法弄清楚的是为什么release那里有额外的东西.由于外部for循环,currentSched.daysOfWeek被调用的次数确实有所不同 - 它在问题对象上被调用3次而在健康对象上被调用5次,但额外的release是在第一次被调用时出现,所以我不知道如何这会影响它.

这些额外信息是否有助于任何人了解正在发生的事情?

Ele*_*zar 0

所以,我已经找到了如何防止这种情况发生,尽管我仍然很困惑为什么它会产生影响。在每个发生额外发布的地方,它都是一个循环。在那些地方,我将属性声明从 for 循环中取出,将其分配给 for 循环中使用的本地变量,现在它工作得很好!所以,以前的一行是:

for (NSString *day in schedule1.daysOfWeek)
Run Code Online (Sandbox Code Playgroud)

我改成两行:

NSArray *daysOfWeek = schedule1.daysOfWeek;
for (NSString *day in daysOfWeek)
Run Code Online (Sandbox Code Playgroud)

显然,这将对所需的保留/释放调用产生影响,但我不明白为什么它最终会对最终保留计数产生影响...如果有人能够深入了解为什么这有帮助,我我很想听听!