---
title: "performSelector may cause a leak because its selector is unknown"  
description: "performSelector may cause a leak because its selector is unknown"  
author: "Anonymous User"  
published: 2015-04-25  
updated: 2015-04-25  
canonical: https://www.mindstick.com/forum/23136/performselector-may-cause-a-leak-because-its-selector-is-unknown  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# performSelector may cause a leak because its selector is unknown

I'm getting the following [warning](https://answers.mindstick.com/blog/77/psychological-risks-of-ai-chatbots-what-experts-are-warning-about) by the [ARC](https://www.mindstick.com/articles/12973/protected-metal-arc-welding-machine) [compiler](https://www.mindstick.com/articles/27/just-in-time-compiler):

```
"performSelector may cause a leak because its selector is unknown".
```

Here's what I'm doing:\

```
[_controller performSelector:NSSelectorFromString(@"someMethod")];
```

Why do I get this warning? I understand the compiler can't [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) the [selector](https://www.mindstick.com/interview/23419/action-selector-in-mvc) [exists](https://www.mindstick.com/forum/158947/how-do-you-use-the-exists-operator-to-check-for-the-existence-of-records-in-a-subquery) or not, but why would that cause a leak? And how can I change my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) so that I don't get this warning anymore?\

## Replies

### Reply by Anonymous User

The compiler is warning about this for a reason. It's very rare that this warning should simply be ignored, and it's easy to work around. Here's how:\
SEL selector = NSSelectorFromString(@"someMethod");IMP imp = [_controller methodForSelector:selector];void (*func)(id, SEL) = (void *)imp;func(_controller, selector);Or more tersely (though hard to read):\
SEL selector = NSSelectorFromString(@"someMethod");((void (*)(id, SEL))[_controller methodForSelector:selector])(_controller, selector)\
**Explaination**\
What's going on here is you're asking the controller for the C function pointer for the method corresponding to the controller. All NSObjects respond to methodForSelector:, but you can also use class_getMethodImplementation in the Objective-C runtime (useful if you only have a protocol reference, like id<SomeProto>). These function pointers are called IMPs, and are simple typedefed function pointers (id (*IMP)(id, SEL, ...))1. This may be close to the actual method signature of the method, but will not always match exactly.\
Once you have the IMP, you need to cast it to a function pointer that includes all of the details that ARC needs (including the two implicit hidden arguments self and _cmd of every Objective-C method call). This is handled in the third line (the (void *) on the right hand side simply tells the compiler that you know what you're doing and not to generate a warning since the pointer types don't match).\
Finally, you call the function pointer.


---

Original Source: https://www.mindstick.com/forum/23136/performselector-may-cause-a-leak-because-its-selector-is-unknown

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
