简体   繁体   中英

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?

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];

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM