gpt4 book ai didi

iphone - PerformSelectorInBackground 导致 EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-29 05:01:23 26 4
gpt4 key购买 nike

好吧,我遇到了一个看似非常简单的情况,但无法弄清楚。

在我的 viewDidLoad 中,我有:

-(void)viewDidLoad
{
[super viewDidLoad];

WestiesModel *model = [WestiesModel new];
[model FetchServerData];
} // I know, I'll move the model creation out of the view controller later

我的 WestiesModel.h 是

 @interface WestiesModel : NSObject
{
NSURL *ServerURL;
NSMutableArray *Westies;
}
@property (retain) NSURL *ServerURL;
-(void) FetchServerData;
@end

在 WestiesModel.m 中,我有

-(void)FetchServerData
{
[self performSelectorInBackground:@selector(backgroundFetchServerData) withObject:nil];
}

-(void)backgroundFetchServerData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSLog(@"%@ %d",Westies,[self retainCount]);
[Westies addObject:@"test"];

NSLog(@"%@ %d",Westies,[self retainCount]);
[Westies addObject:@"test"];

NSLog(@"%@ %d",Westies,[self retainCount]);
[Westies addObject:@"test"];

NSLog(@"%@ %d",Westies,[self retainCount]);
[Westies addObject:@"test"];

.
.
.

[pool release];
}

我在backgroundFetchServerData函数中随机点得到EXC_BAD_ACCESS。就像模型对象正在消失一样,但我只是在 viewDidLoad 函数中泄漏了它。我不会离开这个屏幕或其他任何东西。我正在使用iOS5测试版,在模拟器上运行。

这里有什么想法吗?

最佳答案

I get the EXC_BAD_ACCESS at random points in the backgroundFetchServerData function. Its like the model object is going away, but I just leak it in my viewDidLoad function. I don't transition off this screen or anything. I'm on the iOS5 beta, running on the simulator.

iOS5 beta 已签署 NDA;不能在这里讨论具体细节。但是,没必要。

如果您在并发执行某些操作时随机发生崩溃,那是因为您几乎总是没有正确执行并发操作。

在这种情况下,您可能没有保留要在线程中使用的所有对象(在生成线程之前),并且主线程的自动释放池已耗尽,导致在看似随机的时间点崩溃.

如果没有看到更多代码,就不可能说。不过,还有几点:

  • 不要调用retainCount,因为它没有用。

  • 方法应以小写字母开头

  • 如果“模型”指的是“核心数据模型”,那么您需要阅读核心数据并发指南,因为随机分拆线程是行不通的。看起来您没有使用 CD,所以不用担心这部分。

  • 实例变量应以小写字母开头

这看起来像是线程问题或内存管理问题(或两者兼而有之)。

关于iphone - PerformSelectorInBackground 导致 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6824940/

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