2

I'm developing a custom iOS SDK. I'm creating weak, strong references based on my requirement. What is not clear to me is: when will the weakly reference object gets de-allocated?

Assume I have 3 objects A,B,C. A has a weak reference to B and B has strong reference to C.

A --- >(weak ref) B --->(strong ref)C. All the 3 objects are In-memory objects. Since B does not have any strong references to it, it may get de-allocated. Once B is deallocated, C object memory leaks.I cant have strong ref from "c" back to "B" to prevent it from getting de-allocated as it may cause retain cycles. How will ARC takes a decision to de-alloc B? I do understand that objects will be deallocated immediately when the last strong reference to them goes away.But there is no objects strongly refering to "B" at any cause.In this case,When will B gets de-allocated?.

Karthik207
  • 493
  • 9
  • 27

4 Answers4

3

Weakly referenced objects will be deallocated immediately when the last strong reference to them goes away. Note that that may not be exactly when your last strong reference goes away, if the object has been retained and autoreleased by ARC (essentially creating an additional temporary strong reference).

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84
  • When i create "B" i purposely make it a weak reference.It does not have objects strongly refering to it,at any cause. – Karthik207 Feb 12 '14 at 07:50
  • I do understand that objects will be deallocated immediately when the last strong reference to them goes away.But there is no objects strongly refering to "B" at any cause.In this case,When will B gets de-allocated?. – Karthik207 Feb 12 '14 at 07:54
  • 1
    If there are never any strong references, it will be deallocated immediately. – Catfish_Man Feb 12 '14 at 08:34
3

In your example, since B does not have any strong references to it, it can be deallocated at any time, possibly immediately. When B get deallocated, C does not have any strong references to it which will cause it to be deallocated as well, so it not leak.

It is important to understand that ARC is not garbage collection, in that it doesn't run a process at runtime, collecting the objects that are not needed. It works at compile time, inserting dealloc's in your code when the object no longer pointed at.

kkarayannis
  • 190
  • 8
  • So what happens if "C" has one more strong reference,other than "B"?.So in this case when "B" is de-allocated , the "C" object will be leaked right? – Karthik207 Feb 14 '14 at 05:05
  • You mean that one other object, say D, points to C? No, that would not be a leak since C is still accessible from D and C will be dealloc'ed when D is dealloc'ed. – kkarayannis Feb 14 '14 at 08:24
1

Xcode gives you a hint about this. I declare a weak property:

@property (weak) NSString *weakString;

Then in viewDidLoad I assign a value to it:

self.weakString = [[NSString alloc] init];

Xcode marks that line with a warning that says:

Assigning retained object to weak property; object will be released after assignment

The dude
  • 7,896
  • 1
  • 25
  • 50
0

If there is no object to strongly refer to "B", "B" will be deallocated. but, It is not sure if "B" will be deallocated immediately or not.

In your case with ARC, I assume that the "B" is an instance variable of "A", B would be deallocated immediately when "A" is deallocated. However, if "B" came to be registered in autorelease pool by ARC at any time, "B" will be deallocated at "next run loop cycle".

Kyokook Hwang
  • 2,682
  • 21
  • 36
  • Yeah,"B" is a property in "A",and "C" is property in "B"."A" has a weak reference of "B" and "B" has a strong reference of "C". – Karthik207 Feb 12 '14 at 08:19
  • @Karthik207 I know your question is that when "B" would be deallocated if nothing to refer it. If it is right, "B" will be deallocated immediately as soon as "A"'s `dealloc` method is called. – Kyokook Hwang Feb 12 '14 at 08:30