Is the self in a method in ARC?

I have a method that sometimes crashes.

-(void)foo{ [self doSomething]; [self.delegate didFinish]; [self doSomethingElse]; } 

-doSomething works correctly, then I invoke the -didFinish delegate. Inside -didFinish, a reference to this object can be set to zero, freeing it under ARC. When a method fails, it does it on -doSomethingElse. My assumption was that the self would be strong in the framework of a method that allows you to perform a function. Am I weak or strong? Is there any documentation on this? What would be the reason for the strong or the weak?

Edit

When I was inspired by some of the answers below, I did some investigation. The actual cause of the accident in my case was that the NSNotificationCenter does not save the observer in any way. Mike Weller points out below that callers must save the object during its call to prevent the case described above, however, it seems NSNotificationCenter ignores this problem and always maintains a weak observer reference. In other words:

 -(void)setupNotification{ //observer is weakly referenced when added to NSNotificationCenter [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:SomeNotification object:nil]; } //handle the notification -(void)handleNotification:(id)notification{ //owner has reference so this is fine [self doSomething]; //call back to the owner/delegate, owner sets reference to nil [self.delegate didFinish]; //object has been dealloc'ed, crash [self doSomethingElse]; } 
+6
source share
2 answers

self will be strong within the method, allowing functions to complete. Is weak or strong?

self is neither strong nor weak in ARC. It is assumed that the caller has a link, and self unsafe without support .

It is also true that self can be -dealloc ed as part of its own method in ARC, and it is considered as β€œUndefined Behavior (or at least dangerous)” for your program to do this.

What will be the reason for the strong or the weak?

It is not available for Performance - to avoid the fact that (in the vast majority of cases) an extra inc / dec reference counter. Even if they performed all these additional recounting operations, your program would still be susceptible to such problems in multithreaded programs or in the presence of a race condition (also UB). Thus, this is one of those extreme cases that they (rightfully) have determined that they do not need to defend themselves.

Is there any documentation on it?

Sure!:)

+9
source

self is neither weak nor strong. If you can access self , then you are in the scope of the method call, and this method call is made by someone through a link that they must own. self implied as a valid link as long as it is in scope, and it is understood that any memory management or ownership is handled by the caller.

When calling a method through a weak link, ARC will save the object for the duration of the call to this method (see this answer ). With the strict compiler warning turned on, you really will be forced to create a strong link before sending any methods to this link.

Therefore, by definition, if a method is called on an object, the caller must have ownership, and nothing needs to be done.

Of course, in the end, you can call the calling methods on the freed objects, but this is the result of a bad caller code.

+4
source

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


All Articles