On Dec 27, 2012, at 21:58 , Rick Aurbach <r...@aurbach.com> wrote:

> Rick,
> 
> I'm no Obj-C language expert; in fact I'm pretty much a newbie. But I have 
> had some experience with smart pointers (boost, etc) from my C++ case. 
> 
> Here's how I think of strong and weak references in ARC. (If the following is 
> wrong, I'm sure someone more knowledgeable than myself will set us both 
> straight.)
> 
> Weak references never increment the retain count. 
> 
> They are simply pointers which are automatically nil-ed when the object's 
> retain count reaches 0 (and is hence deallocated). So, in your method, you 
> create an object with [NSMutableData data] and assign it to a weak pointer. 
> It's retain count is 1. At the end of the method, this object has its retain 
> count decremented. Since its count is now 0, it is deallocated and the weak 
> reference to it is set to 0.
> 
> In your case, this is confusing because you've combined two statements into 
> one. The logic of your line of code is really
>       id temp = [NSMutableData data];
>       mInputBuffer = temp;
> 
> In this formulation, it is clear that the temp is deallocated when the method 
> exits. Combining these two statement in a way that hides the temporary does 
> not automagically promote the weak pointer to being a strong one.
> 
> Hope this helps.

Not really. I may not have been clear. This is in an Objective-C object, just 
compiled in a .mm file so I can call some other C++ code. But the object in 
question, and the ivar mInputBuffer, are all Obj-C.

So, the usual bit about references being weak by default in C structs (and C++ 
classes) doesn't hold here.

> 
> 
> On Dec 27, 2012, at 2:00 PM, cocoa-dev-requ...@lists.apple.com wrote:
> 
>> I just wrote some Objective-C++ code. It's basically just a regular Obj-C 
>> class, but makes some C++-style calls in its methods.
>> 
>> Anyway, I added an NSMutableData* ivar to my class, and set it with
>> 
>>      mInputBuffer = [NSMutableData data];
>> 
>> in one of the methods. Subsequent accesses, however, have resulted in it 
>> being deallocated, even though I never set mInputBuffer to NULL anywhere.
>> 
>> I changed the code to refer to it via a strong property, and it's fine.
>> 
>> I thought ARC worked the same in both cases (that is, retained the object on 
>> assignment and kept it around until the last reference was gone, even if via 
>> a regular ivar)
>> 
>> A bit of googling confirms this, so I'm wondering what I've done wrong.
>> 
>> -- 
>> Rick
> 
> Cheers,
> 
> Rick Aurbach, Ph.D.
> 
> 
> 
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/rmann%40latencyzero.com
> 
> This email sent to rm...@latencyzero.com


-- 
Rick




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to