gpt4 book ai didi

ios - iPhone NSMutableArray 和 NSKeyedUnarchiver unarchiveObjectWithFile 发布异常

转载 作者:行者123 更新时间:2023-11-28 20:47:47 25 4
gpt4 key购买 nike

我将实现 .一旦我将它从文件加载到保留属性@property (nonatomic, retain) NSMutableArray *伙伴;对象的释放计数是 2(正确,它是 1of autorelease + 1 of retain of the property)但是没有人释放它并且保留计数变为 1,所以当我释放它时我得到-[__NSArrayM retainCount]: 发送到释放实例的消息(我认为因为 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];
}

有人知道为什么会这样吗?

最佳答案

没有比这更好的了:

The number returned by retainCount is useless.

不要依赖它做任何事。使用 Instruments 中的 Leaks 工具来确定您是否正在泄漏对象。


如果你崩溃了,很可能你遇到了僵尸。参见 this video了解如何使用 Instruments 寻找僵尸。

关于ios - iPhone NSMutableArray 和 NSKeyedUnarchiver unarchiveObjectWithFile 发布异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3885368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com