开发者

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 tha开发者_运维百科t 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.


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


[NSPropertyListSerialization propertyListFromData:(NSData *) mutabilityOption:(NSPropertyListMutabilityOptions) format:(NSPropertyListFormat *) errorDescription:(NSString **)]

If you're working with something like this returns

(NSString **)

then you should check documentation that clearly states if you're required to release it. If documentation does not state anything about you releasing returning object like

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

then don't wory about them, they will be released when appropriate or have already been autoreleased by framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜