问题描述
我有以下代码来检索我的iOS应用上的文件路径:
I have the following code to retrieve file paths on my iOS Apps:
static const NSString * fullPathFromRelativePath(NSString *relPath)
{
// do not convert a path starting with '/'
if(([relPath length] > 0) && ([relPath characterAtIndex:0] == '/'))
return relPath;
NSMutableArray *imagePathComponents = [NSMutableArray arrayWithArray:[relPath pathComponents]];
NSString *file = [imagePathComponents lastObject];
[imagePathComponents removeLastObject];
NSString *imageDirectory = [NSString pathWithComponents:imagePathComponents];
NSString *fullpath = [[NSBundle mainBundle] pathForResource:file
ofType:NULL
inDirectory:imageDirectory];
if (!fullpath)
fullpath = relPath;
return fullpath;
}
static const char * fullCPathFromRelativePath(const char *cPath)
{
NSString *relPath = [NSString stringWithCString:cPath encoding:NSUTF8StringEncoding];
const NSString *path = fullPathFromRelativePath(relPath);
const char *c_path = [path UTF8String];
return c_path;
}
static const char * relativeCPathForFile(const char *fileName)
{
NSString *relPath = [NSString stringWithCString:fileName encoding:NSUTF8StringEncoding];
const NSString *path = fullPathFromRelativePath(relPath);
const char *c_path = [[path stringByDeletingLastPathComponent] UTF8String];
return c_path;
}
我在调试控制台中收到了很多这样的消息:
I'm getting quite a few messages like this in the debug console:
objc[4501]: Object 0x6e17060 of class __NSCFString autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[4501]: Object 0x6e12470 of class NSPathStore2 autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
objc[4501]: Object 0x6e12580 of class __NSCFData autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
代码有什么问题? (我什至在使用带有自动"保留/释放功能的iOS 5等)
What's the problem with the code ? ( I'm even using iOS 5 with "automated" retain/release, etc )
干杯.
推荐答案
当您在其堆栈中没有任何释放池的线程上自动释放对象时,将显示此消息.默认情况下,主线程上始终有一个自动释放池.它是在通常由应用程序的main()函数调用的UIApplicationMain()
函数中创建和管理的.但是,您创建的其他线程(使用performSelectorInBackground:
或NSThread
)没有适当的自动释放池,除非您专门在其中放置了一个自动释放池,因此该后台线程上的任何自动释放的对象都没有池供以后释放它们,而只是泄漏.
This message shows up when you autorelease an object on a thread that does not have any release pools in its stack. By default, there is always an autorelease pool on the main thread. It's created and managed within the UIApplicationMain()
function that is usually called by your app's main() function. However, additional threads you create (with performSelectorInBackground:
or NSThread
) do not have an autorelease pool in place unless you specifically put one there, so any autoreleased objects on that background thread have no pool to release them later, and will just leak.
如果要启动后台线程,则要做的第一件事就是创建一个自动释放池.在ARC下,使用新的@autoreleasepool
构造进行此操作.
If you're kicking something off to a background thread, the first thing you should do is create an autorelease pool. Under ARC, use the new @autoreleasepool
construct to do that.
这篇关于为什么静态NSString泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!