TechChain
TechChain

Reputation: 8944

Perform selector may cause leak because select is unknown?

I am using the below code & i am getting warning as

Perform selector may cause leak because select is unknown

Code

SEL _selector = NSSelectorFromString([[arrEffects objectAtIndex:1] valueForKey:@"method"]);
self.mainImageView.image = [self.mainImage performSelector:_selector];

Please tell me who can i get rid of this warning?

Upvotes: 0

Views: 886

Answers (3)

gnasher729
gnasher729

Reputation: 52538

You fix it by getting rid of this awful code. To whoever thought this code was a good idea: No, it's not. It's an attempt at being clever and failing miserably.

You are storing the name of a method somewhere in an array, which is totally unsafe. I recommend you analyse the code, find which selectors are possibly stored, and instead of this awful code you store a block which takes an image as a parameter and returns an image instead of the selector, then call the block.

Upvotes: -1

Abhinav
Abhinav

Reputation: 38162

The reason for this warning is that with ARC, the runtime needs to know what to do with the result of the method you're calling. The result could be anything: void, int, char, NSString *, id, etc. ARC normally gets this information from the header of the object type you're working with.3

There are really only 4 things that ARC would consider for the return value:4

  1. Ignore non-object types (void, int, etc)
  2. Retain object value, then release when it is no longer used (standard assumption)
  3. Release new object values when no longer used (methods in the init/ copy family or attributed with ns_returns_retained)
  4. Do nothing & assume returned object value will be valid in local scope (until inner most release pool is drained, attributed with ns_returns_autoreleased)

This thread explains it in length.

Upvotes: 1

ullstrm
ullstrm

Reputation: 10160

The warning is there for a reason. You can check if mainImage responds to selector before performing it. The warning will not go away, but it is more safe. Something like this:

 SEL _selector = NSSelectorFromString(@"asdf");
    if([self respondsToSelector:_selector])
        [self performSelector:_selector];

Upvotes: 2

Related Questions