使用[NSThread detachNewThreadSelector:toTarget:withObject:]时如何设置自动释放池 [英] How to set up an autorelease pool when using [NSThread detachNewThreadSelector:toTarget:withObject:]

查看:609
本文介绍了使用[NSThread detachNewThreadSelector:toTarget:withObject:]时如何设置自动释放池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用[NSThread detachNewThreadSelector:toTarget:withObject:]而且我收到了大量内存泄漏,因为我没有为分离线程设置自动释放池。我只是想知道我在哪里这么做?是在我打电话之前

Hi I'm usuing [NSThread detachNewThreadSelector:toTarget:withObject:] and I'm getting a lot of memory leaks because I have no autorelease pool set up for the detached thread. I'm just wondering where I actualy do this? Is it before I call

[NSThread detachNewThreadSelector:toTarget:withObject:]

或在另一个线程中运行的方法?

or in the method that is being ran in the other thread?

任何帮助将不胜感激,一些示例代码会很棒。

Any help would be appreciated, some sample code would be great.

谢谢。

推荐答案

在你用线程调用的方法中...即给定...

in the method you call with the thread... i.e. given this...

[NSThread detachNewThreadSelector:@selector(doStuff) toTarget:self withObject:nil];

您的方法是......

Your method would be...

- (void)doStuff {
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  //Do stuff
  [pool release];
}

这篇关于使用[NSThread detachNewThreadSelector:toTarget:withObject:]时如何设置自动释放池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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