I thought I had a pretty good handle on memory management for objective-c, but I can’t figure out the following situation:
@protocol MyProtocol
@end
@interface MyObject : NSObject {
id<MyProtocol> reference;
}
@property (nonatomic, retain) id<MyProtocol> reference;
@end
@implementation MyObject
@synthesize reference;
-(void) dealloc {
[reference release];
[super dealloc];
}
...
@end
This gives me a “warning: ‘-release’ not found in protocol(s)“.
Can I safely ignore this error? Or am I doing something horribly wrong?
Yes you can safely ignore this error. An object declared as type
id<MyProtocol>may not inherit fromNSObject(you do not have to use the Cocoa libraries to program in Objective-C and there are other root classes even in Cocoa such asNSProxy). Sinceretain(andrelease,autorelease) are declared inNSObject, the compiler cannot know that the instance declared as typeid<MyProtocol>responds to these messages. To get around this, Cocoa also defines theNSObjectprotocol which mirrors theNSObjectAPI. If you declare your protocol asindicating that
MyProtocolextends theNSObjectprotocol, you’ll be set.