我有下面的代碼在我的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