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"); 
4
  • 3
    strong and weak are useful with ARC code, not MRC code. Commented Sep 8, 2015 at 15:39
  • 1
    Turn-on ARC! You can't write retain and release with ARC enabled. Commented Sep 8, 2015 at 15:44
  • @Cy-4AH - I turned off ARC because I wanted to demonstrate __strong and __weak with code. All the examples I see of strong and weak are with properties. Commented Sep 8, 2015 at 15:49
  • 1
    @rtsao, like you noticed: __strong and __weak have no effect in MRC. Commented Sep 8, 2015 at 16:01

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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 No, strong and weak are not just for properties. But they are just for ARC, not MRC.
@rmaddy got it, can you give an example for such usage? thanks
@rtsao Do a search here on __weak and __strong and you will find many examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.