2

I was reading about __strong reference and __weak reference usage here: Explanation of strong and weak storage in iOS5

I tried writing a bit of code to demonstrate this knowledge. However, the __strong did not keep the object in memory as it was released. 1st I did this:

Parent *  fumu = [[Parent alloc] init]; 
[fumu release]; 

Everything works as expected. Parent object init gets called, When released, the dealloc gets called.

2nd I did this:

Parent *  fumu = [[Parent alloc] init]; 
[fumu retain];
[fumu release]; 

The Parent object init method was called. But dealloc was not called because the Parent object that fumu references still has retain count of 1. As expected.

Using __strong

As stated:

**Strong: "keep this in the heap until I don't point to it anymore"
Weak: "keep this as long as someone else points to it strongly"**

Now let's say I use __strong keyword. If I add another strong reference like below, the Parent object should NOT call dealloc because we still have a strong reference (anotherFumu) to it. However, when I run it, the dealloc gets called. I do not see the strong reference having any effect.

Parent *  __strong fumu = [[Parent alloc] init]; 
Parent *  __strong anotherFumu = fumu;  
[fumu release];  //Parent object dealloc gets called

Please advise. thanks

Result:

I turned on ARC, and simply used nil to point the strong pointers away from the Parent object, and thus be able to correctly see the behavior of __strong and __weak like so:

Parent * fumu = [[Parent alloc] init];
__strong Parent * strongFumu = fumu;
__weak Parent * weakFumu = fumu;

fumu = nil; //your auto variables window should show that both trongFumu and weakFumu are still valid with an address
NSLog(@"weakFumu should be valid here, because strongFumu is still pointing to the object");

strongFumu = nil; //when strongFumu points away to nil, weakPtr will then also change to nil
NSLog(@"weakFumu should be nil here");
Community
  • 1
  • 1
rtsao
  • 199
  • 1
  • 12

1 Answers1

2

alloc is short for allocate so when you call
Parent * fumu = [[Parent alloc] init];
you allocate the object so its retain count is =1 then you call [fumu retain];
your objects retain count is up to +2
then when you call [fumu release];
it adds -1 so your final count will be +1 so that is right.

Strong and Weak are ARC types, you cant use them in non-ARC projects. And it is used with properties/variables... You would want to use strong when you need to own the object, you are already "owning" the object when you create the object in your example...

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW1

u.gen
  • 4,757
  • 4
  • 39
  • 76
  • ok, so __strong/__weak or strong/weak types are only used with @property ? that means I can't do this? __strong MyClass * ptr = .... IF we can use them in that fashion, can you give an example? – rtsao Sep 08 '15 at 16:12
  • @rtsao No, `strong` and `weak` are not just for properties. But they are just for ARC, not MRC. – rmaddy Sep 08 '15 at 16:13
  • @rmaddy got it, can you give an example for such usage? thanks – rtsao Sep 08 '15 at 16:19
  • @rtsao Do a search here on `__weak` and `__strong` and you will find many examples. – rmaddy Sep 08 '15 at 16:21