Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARC, self and blocks

I thought I understood the usage of self in a block that is copied is a no no.

But in an attempt to clean my code i enabled a bunch of warnings in Xcode, one called "Sending messages to weak pointers"

so now in all my blocks, every time I use my created weakself reference __weak typeof(self) weakself = self;

I get this warning: Weak receiver may be unpredictably set to nil

a trivial example:

__weak typeof(self) weakself = self;
[aClass doSomethingInABlock:^{

     [weakself doSomething]; //warning.

}];

I have seen answers which define a version of self within the block like so:

__weak typeof(self) weakself = self;
[aClass doSomethingInABlock:^{
     typeof(self) selfref = weakself; 
     [selfref doSomething]; //no warning.

}];

So I'm wondering what actually happens here:

  • Am I just tricking the compiler?
  • what does a strong reference to a weak reference do?
  • anything else I'm missing.

Thanks.

like image 355
cream-corn Avatar asked Mar 12 '14 00:03

cream-corn


People also ask

What is meant by arc in psychology?

The Attachment, Regulation and Competency (ARC) Framework is a flexible, components-based intervention developed for children and adolescents who have experienced complex trauma, along with their caregiving systems.

What is the arc designed to do?

The ARC framework is designed to address areas of life negatively affected by complex trauma: mainly, attachment patterns, self-regulation, and basic developmental skills.

Why do you generally create a weak reference when using self in a block?

For many of us, it's best practice to always use weak combined with self inside closures to avoid retain cycles. However, this is only needed if self also retains the closure. By adding weak by default you probably end up working with optionals in a lot of cases while it's actually not needed.

Which of the following can cause a retain cycle?

Structs and Classes A choice between a struct and a class can cause a retain cycle. Both structs and classes can have constants, variables, functions and protocols.


1 Answers

I thought I understood the usage of self in a block is a no no.

This is not strictly correct. Blocks retain the objects in them, so don't use self in a block if your block is retained by self.

For example, you can use self just fine in a UIView animation block. This is because your view controller (or whatever code is calling the animation) doesn't have a pointer to the UIView animation block.)

Am I just tricking the compiler?

No.

What does a strong reference to a weak reference do?

If the weak reference is not nil, the retain count of the receiver is increased. This will stop the object from being deallocated while you're using it.

Remember, ARC will deallocate objects when there are no longer strong references to them. By creating a strong reference inside the block, you're preventing possible deallocation until you're done with it.

anything else I'm missing.

I recommend reading the Practical Memory Management section of the Advanced Memory Management Programming Guide. Especially, read the subsection "Use Weak References to Avoid Retain Cycles".

like image 95
Aaron Brager Avatar answered Oct 13 '22 10:10

Aaron Brager