"performSelector can cause a leak" when it leaks?

I need to execute a selector by name in the class (and not in the instance) and use its return value:

id obj = [objClass performSelector:NSSelectorFromString(methodName) withObject:p1]; 

The selector creates a new instance of the class. I need to use a return instance. Obviously, what I get is a regular performSelector that can cause a leak because its selector is unknown warning, because this project is compiled with ARC.

If I understood correctly ( from the answers here and others), in this case, the performSelector function will cause a leak (correct me if I am wrong, then I could just turn off the warning and do it). Selectors are implemented as follows:

 + (id) objectWithFile:(NSString*)p1 { return [NSKeyedUnarchiver unarchiveObjectWithFile:p1]; } 

What are my options when I have to use a selector from a string, and the selector creates and returns a new instance of the object?

I considered NSInvocation, but its getReturnValue method requires me to provide my own dedicated buffer that stores the return value. I'm not sure if this even works with ARC and class methods, or just need __bridge_transfer to __bridge_transfer return buffer to id and all that it needs.

+6
source share
1 answer

objectWithFile: not a "alloc, copy, init, mutableCopy and new family" method and, therefore, the "Invalid return values" method in the sense of "Clang / ARC Documentation" :

A method or function that returns the type of the stored object, but does not return the stored value, you need to make sure that the object is still valid across the return border.
...
In the worst case, this may include automatic notification, but callers should not assume that this value is indeed in the auto-detection pool.

Thus, no matter what you do inside the method, the caller does not need to release the returned object.

Therefore, I do not think you have a memory leak in your code.

+5
source

Source: https://habr.com/ru/post/950965/


All Articles