我有下面的代码在我的iOS应用检索文件路径:为什么静态NSString泄漏?
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;
}
我得到了不少这一类的邮件在调试控制台:
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与“自动”保留/释放等)
干杯。
嘿那里,这个答案是正确的。我没有通过任何cocoa API创建更多的线程,但是我有一些新的线程由较低级别的C函数创建,这是造成这个问题的原因。我围绕@autoreleasepool封装了第一个方法,问题得到解决。 还在围绕着ARC环顾我的头:) – Goles