Gol*_*les 4 iphone memory-leaks objective-c ios
我有以下代码来检索我的iOS应用程序上的文件路径:
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;
}
Run Code Online (Sandbox Code Playgroud)
我在调试控制台中收到了很多这样的消息:
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
Run Code Online (Sandbox Code Playgroud)
代码有什么问题?(我甚至使用iOS 5进行"自动"保留/释放等)
干杯.
BJ *_*mer 11
当您在其堆栈中没有任何发布池的线程上自动释放对象时,将显示此消息.默认情况下,主线程上始终存在自动释放池.它是在UIApplicationMain()应用程序的main()函数通常调用的函数中创建和管理的.但是,您创建的其他线程(使用performSelectorInBackground:或者NSThread)没有自动释放池,除非您专门放置一个,因此该后台线程上的任何自动释放的对象都没有池以便稍后释放它们,并且只会泄漏.
如果你正在向后台线程踢一些东西,你应该做的第一件事就是创建一个自动释放池.在ARC下,使用新@autoreleasepool构造来执行此操作.