我正在开发一个应用程序,其中我发现内存泄漏的方法如何我删除泄漏?
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attributes
{
if ((self = [super init]))
{
_buffer = [str mutableCopy];
_attributes = [NSMutableArray arrayWithObjects:[ZAttributeRun attributeRunWithIndex:0 attributes:attributes], nil];
}
return self;
}
我在这条线附近发现泄漏“ _buffer = [str mutableCopy] ;”
在分配堆栈跟踪中,我发现同时内存分配增加为CFString。
感谢。
答案 0 :(得分:2)
如果您在[_buffer release]
方法中添加一行dealloc
,我认为您不会有内存泄漏。您有一个分配,因为对于包含alloc
,retain
和copy
等内容的每个方法,您都会创建一个新的对象实例。在这种情况下,这没关系。
您必须担心的另一件事是_attributes
对象的内存崩溃。你拥有一个自动释放的对象,下次你尝试使用它时,它可能已经被释放了
答案 1 :(得分:1)
mutableCopy保留了返回的对象,因此您有责任在完成后释放它。这符合Memory Management Rules。