Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I release the NSError object of NSFileManager's copyItemAtPath:toPath:error:?

NSFileManager has a method to do copying.

- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error

If an error occurs, the third parameter (NSError **) upon return will contain an NSError object describing the problem.

Question: do I need to release it?

There are some other methods, for example this one takes (NSString **),

NSPropertyListSerialization +(NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format errorDescription:(NSString **)errorString

do they follow the same memory management rules? To release or not to, that's the question.

---Answer

As Anders said, the answer is "not" to release.

I got confused because the class NSPropertyListSerialization has a method

+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format errorDescription:(NSString **)errorString

the document says that I should release the third argument if not nil. However it's deprecated and replaced by

+ (NSData *)dataWithPropertyList:(id)plist format:(NSPropertyListFormat)format options:(NSPropertyListWriteOptions)opt error:(NSError **)error

and the argument is (NSError **) now. No need to release as other similiar methods. So the general memory manegement rule is no need to release this kind of arguments.

---Reference document

In Apple's Advanced Memory Management Programming Guide, section You Don’t Own Objects Returned by Reference:

When you invoke any of these methods, you do not create the NSError object, so you do not own it.

like image 354
yehnan Avatar asked Dec 20 '25 15:12

yehnan


1 Answers

The returned NSError object is an autoreleased object so you shouldn't release it

The argument just tells the function where to put the returned error object (if any)

EDIT: can't spell today it seems

like image 140
AndersK Avatar answered Dec 23 '25 12:12

AndersK