Avoiding static analyzer leak warnings when using objc_msgSend?

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:

  1. 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.

  2. I cannot change the name of any methods.

  3. 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


see https://stackoverflow.com/a/5833430/1313031 for a way to suppress static analyzer warnings in code

But yeah, the best thing to do would be to rename newInstance

Need Your Help

How can I databind to properties not associated with a list item in classes deriving List<T>

c# generics data-binding

Previously, I had a class that wrapped an internal System.Collections.Generic.List&lt;Item&gt; (where Item is a class I created). The wrapper class provided several collection-level properties that

Searching through a collection of an array list pair

java if-statement collections arraylist iterator

I am trying to search through a collection of an ArrayList if pairs. What I want to be able to do, is to go through the collection and find the first value in a pair and return the second value of ...