什么是“自动释放,没有池在适当位置”是什么意思? [英] What does “Autoreleased with no pool in place” mean?
问题描述
我的应用程序结构如下,
核心部分用C ++编写,使用线程,我正在开发UI的Objective C,
如果我不执行线程它工作正常,但我不能禁用,停止线程,UI是崩溃在日志中随机可以看到,以下消息
__ NSAutoreleaseNoPool():NSCFString类的对象0x350270自动释放,没有池 - 正好泄露
消息来多一次,
通过googling来知道,我需要设置NSAutoReleasePool摆脱它,但如何可能与C ++代码集成。
可以在UI中调用AutoReleasePool
任何人都可以指导我?
查看这些文档,了解使用Cocoa的多线程应该知道的内容: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html p>
您可以像设计应用程式那样设计应用程式,但请注意以下两点:
- 当UI控件(如视图(AppKit或UIKit))在主线程上操作时,生命是最简单的(有时是必需的)。您可以在后台线程中使用Foundation对象和一些AppKit / UIKit对象,并且可以在多个线程中使用一些Foundation对象。
- 如果您在后台线程中使用任何Cocoa对象,您需要在这些线程上设置自动释放池。
如此:
- (void)backgroundThreadStart
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// do stuff
[pool release];
}
这将修复您的控制台错误,但您可能有其他问题导致你看到的实际崩溃。
My Application structure is as follows, the core part is written in C++ and using thread heavily, and i am developing UI in Objective C on top of it, if i don't execute the thread it works fine, but i can't disable, stop thread, UI is crashing randomly in the log i could see , following message
__NSAutoreleaseNoPool(): Object 0x350270 of class NSCFString autoreleased with no pool in place - just leaking
Similar messages coming more then once, by googling come to know, i need to set NSAutoReleasePool to get rid of it, but how its possible to integrate the same with C++ code.
Edit: Core lib will be activated from UI , hence i suppose, its safe to say UI is running in the main thread, Lib is creating/terminating thread without notifying UI, in this case, can i call AutoReleasePool in the UI
Can anyone guide me?
See these docs for what you should know about multithreading with Cocoa: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html
It's OK to design your app like you have, but two things should be kept in mind:
- Life is simplest (and sometimes necessary) when UI controls like views (AppKit or UIKit) are manipulated on the main thread. You can use Foundation objects and some AppKit/UIKit objects on background threads, and some Foundation objects can be used from multiple threads.
- If you're using any Cocoa objects at all in background threads, you'll need to set up autorelease pools on those threads.
Like so:
- (void)backgroundThreadStart
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// do stuff
[pool release];
}
That will fix your console errors, but you might have other issues that led to the actual crashing you were seeing.
这篇关于什么是“自动释放,没有池在适当位置”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!