我有 NSMutableArray* cityData,我用自定义 LocationDetail 对象填充它。 cityData在viewDidLoad中创建并在dealloc中释放。 在代码中的某个位置,根据用户操作,我填充 LocationDetail 并将其添加到 cityData 数组中:
LocationDetail* d = [[LocationDetail alloc] init];
d.city = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 1)];
d.tz = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 3)];
d.country = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 2)];
d._id = [NSString stringWithFormat:@"%S", (char*)sqlite3_column_text16(statement, 0)];
[cityData addObject:d];
[d release];
当我完成 View Controller 并将其删除时,Leaks 实用程序说我在 NSCFString 中的所有 4 行代码中存在泄漏,上面带有 [NSString stringWithFormat]。
我尝试删除 sqlite3 内容并简化对类似内容的调用
d._id = [NSString stringWithFormat:@"%s", "a string"]
结果相同。但是,如果我像这样替换 NSString stringWithFormat:
d._id = @"a string";
泄漏消失了。我想知道为什么如果我使用 stringWithFormat 会出现泄漏,但如果我使用 @"something"则不会。有什么明显的事情表明我做错了吗?
谢谢!
最佳答案
属性不会自动为您释放,您需要自行释放
- (void)dealloc
参见 The Objective-C Programming Language: Declared Properties举个例子。
<小时/>编辑:
该示例似乎已移至 Advanced Memory Management Programming Guide 中.
关于ios - NSString 的内存管理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298624/