1
votes

I'm having trouble with the following code, where Xcode is flagging a memory problem. The warnings are below the code, on the return line. Does anybody know why, and what I can do about it?

- (id)copyWithZone:(NSZone *)zone
{
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:self];
    return [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}

Mvariable.m:177:2: Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected
Mvariable.m:177:9: Method returns an Objective-C object with a +0 retain count
Mvariable.m:177:2: Object returned to caller with a +0 retain count
Mvariable.m:177:2: Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected

1

1 Answers

7
votes

methods that start with "copy", "create", "new", "alloc", or "retain", must return an object that has been retained, ie, the caller must release it.

[NSKeyedUnarchiver unarchiveObjectWithData:archivedData] returns an autorelased object.

see: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html