iphone:线程+释放池+ [对象释放] =“发送到已释放实例的消息" [英] iphone: threads + release pool + [ object release] = "message sent to deallocated instance"

查看:74
本文介绍了iphone:线程+释放池+ [对象释放] =“发送到已释放实例的消息"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone上运行了一些小代码,现在很不开心.

I'm having a bad time with some little code running on the iphone.

基本上,我只是按下一个按钮,它调用runTest,它在后台线程上运行test方法.这就是为什么我创建了一个自动释放池.

Basically, i just press a button, it calls runTest, it runs test method on a background thread. That's why I created an autorelease pool.

如果我运行以下代码,我会在控制台上收到一条漂亮的消息,说:
2010-09-07 11:45:15.527 test[1312:207] *** -[CFString release]: message sent to deallocated instance 0x3d52ba0

if i run the below code i got a beautiful message on the console saying:
2010-09-07 11:45:15.527 test[1312:207] *** -[CFString release]: message sent to deallocated instance 0x3d52ba0

-(void) test {
    NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];

    NSString *xml = [[NSString alloc] initWithFormat:@"<%@>", @"msg"];
    NSLog(@"%@\n",xml);
    [xml release];

    [apool release]; // <-- this line throws the error msg  
}

- (IBAction) runTest: (id)sender
{
    [self performSelectorInBackground:@selector(test) withObject:nil];

}

我发现:如果不在后台线程(没有自动释放池)上运行test,只需调用[self test],代码就可以正常工作.

I have found that: if I do not run test on a background thread (no autorelease pool), just calling [self test], the code works fine.

所以,我认为问题出在线程+自动释放池周围,我在做什么错?以及我该如何解决?

So, i think the problem is around the thread + autorelease pool, what am I doing wrong? and how may I solve it?

P.S.我启用了NSZombie标志.

P.S. I have the NSZombie flag enabled.

推荐答案

您是否在对象xml上尝试了-autorelease?这会将其添加到活动池apool.声明drain比声明-release更好-在非垃圾回收环境中,它们是相同的,但是有一天Apple可能会在iPhone上实现垃圾回收.希望这会有所帮助.

Have you tried -autorelease on the object xml instead? This will add it to the active pool, apool. Declaring drain is better than declaring -release - they're the same in a non-garbage collection environment, but some day Apple may implement garbage collection on the iPhone. Hope this helps.

这篇关于iphone:线程+释放池+ [对象释放] =“发送到已释放实例的消息"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆