我应该释放我创建的NSThread吗? [英] Should I release the NSThread I create?

查看:174
本文介绍了我应该释放我创建的NSThread吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能对此太想了.

// Inside some method...
NSThread *thread = [[NSThread alloc] initWithTarget:functionWrapper selector:@selector(run) object:nil];
[thread start];

在此之后调用[thread release]:A.避免内存泄漏,这是有必要的,或者... B.会破坏东西.

Calling [thread release] after this: A. Avoids a memory leak and is necessary or... B. Will break things.

这个问题的答案建议线程在完成执行后将释放自身,但是该行为记录在哪里?

The answer to this question suggests that the thread will release itself when it finished executing, but where is that behavior documented?

推荐答案

是的,您将需要在一段时间后通过自动释放或释放来释放它.

Yes you will have to release it some time later, either through autorelease or release.

但是,您实际上并不需要一开始就初始化自己的线程,objective-c提供了很多方法来实现线程化而无需自己分配新线程,例如

However, you don't really need to init your own thread in the first place, objective-c has plenty of ways for you to implement threading without allocing a new thread yourself, like

[self performSelectorInBackground:@selector(yourMethod) withObject:nil];

还有NSOperations,您也可以将任务排队.

There are also NSOperations that allow you to queue up your tasks as well.

这篇关于我应该释放我创建的NSThread吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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