我存档了一个自定义对象数组 (NSMutableArray),这些对象实现了 . 一旦我将它从文件加载到保留属性@property (nonatomic, retain) NSMutableArray *buddies; 对象的释放计数为 2(正确,它是自动释放的 1 + 属性保留的 1)但是没有人释放它并且保留计数变为 1,所以当我释放它时,我得到 -[__NSArrayM retainCount]: message sent to解除分配的实例(我认为因为 1 保留计数是自动释放)
这是完整的代码:
BuddieListViewController.h
#import <UIKit/UIKit.h>
#import "Buddie.h"
@interface BuddieListViewController : UITableViewController {
IBOutlet NSMutableArray *buddies;
[...]
}
[...]
@property (nonatomic, retain) NSMutableArray *buddies;
[...]
@end
BuddieListViewController.m
#import "BuddieListViewController.h"
#import "Buddie.h"
#import "PreviewViewController.h"
@implementation BuddieListViewController
@synthesize buddies;
[...]
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
[self loadFromDisk];
}
return self;
}
- (void)loadFromDisk {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *appFile = [documentsPath stringByAppendingPathComponent:@"BuddieArchive.ark"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile]) {
self.buddies = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
NSLog(@"1- buddies retain count %d (should be 2, 1 + 1autorelease)", [buddies retainCount]);
} else {
self.buddies = [NSMutableArray arrayWithCapacity:1];
}
}
[...]
- (IBAction)cancelledBuddie:(NSNotification *)notification
{
[editingBuddie release];
NSLog(@"2- buddies retain count %d (should be 2, 1 + 1autorelease)", [buddies retainCount]);
[buddies release];
[self loadFromDisk];
[self.tableView reloadData];
}
有人知道为什么会这样吗?