I have the following (simplified) code from a generic factory class:
- (id) invokeSetup: (id) object {
// Just an example, subclasses delegate setup to a component that either returns +0 or +1 references
return objc_msgSend(object, @selector(init));
}
- (id) newInstance {
id object = objc_msgSend([NSString class], @selector(alloc));
id replacement = [self invokeSetup: object];
return replacement;
}
The analyzer produces a warning on the return replacement:
:
warning: Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected
I somehow need to tell the analyzer that the reference returned by - invokeSetup
is +1. The above example is simplified, and in the real program, there are a few constraints:
I cannot annotate
invokeSetup
with ns_returns_retained, since it is inherited, and there are other subclasses where invokeSetup returns +0 references. If it is a +1 or +0 can only be detected at runtime.I cannot change the name of any methods.
The design is the way it is. There may be better designs, but that can't be changed here.
Is it possible to somehow tell ARC at the point of assignment (id replacement = ...
) that the reference definitely is +1?
Thanks, Jochen