简体   繁体   中英

How to fix memory leaks for an autoreleased object

I have this method which leaks ~ 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. an object with a retain count of 1. Auto releasing the object returns an object with a retain count of 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.

Small addition to Jasarien answer, you should name your method something like:

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

This would fix your problem and match Cocoa coding style and spirit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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