简体   繁体   English

如何修复自动释放对象的内存泄漏

[英]How to fix memory leaks for an autoreleased object

I have this method which leaks ~ 6KB : 我有这种泄漏〜6KB的方法:

+ (EInspectorFacilityInfo*) newWithNode: (CXMLNode*) node
{
  if(node == nil) { return nil; }
  return (EInspectorFacilityInfo*)[[[EInspectorFacilityInfo alloc] initWithNode: node] autorelease];
}

here is a screenshot indicating the memory leak in instruments. 这是一个截图,指示仪器中的内存泄漏。 在此处输入图片说明

how can I get rid of this memory leak ? 我如何摆脱这种内存泄漏?

The method has the word 'new' in it, so by the Objective-C conventions it is expected to return an owning reference to the object, ie. 该方法中包含“ new”一词,因此根据Objective-C约定,它期望返回对对象的拥有引用,即。 an object with a retain count of 1. Auto releasing the object returns an object with a retain count of 0. 保留计数为1的对象。自动释放该对象将返回保留计数为0的对象。

You must either remove the word new from the method name, or not auto release the object - in which case, the caller will be responsible for releasing it. 您必须从方法名称中删除单词new,或者不自动释放对象-在这种情况下,调用者将负责释放它。

Small addition to Jasarien answer, you should name your method something like: 除了Jasarien答案之外,您还应该将方法命名为:

+ (EInspectorFacilityInfo*) inspectorFacilityInfoWithNode: (CXMLNode*) node

This would fix your problem and match Cocoa coding style and spirit. 这将解决您的问题,并与Cocoa编码风格和精神保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM