On May 13, 2013, at 21:30 , Andy Lee <ag...@mac.com> wrote:

> I believe ARC keeps it alive by virtue of self being a strong reference.

It isn't, not exactly. According to section 7.3 of the Clang ARC spec:

"The self parameter variable of an Objective-C method is never actually 
retained by the implementation. It is undefined behavior, or at least 
dangerous, to cause an object to be deallocated during a message send to that 
object."

This is the case when the receiver of the method invocation is itself a strong 
reference. If it's actually a __weak reference, it *is* retained for the 
duration of the method execution, because of the rules for retention of __weak 
objects used in expressions.

> I did a quick test and found that if I do
> 
> - (void) mouseDown: (NSEvent*) event
> {
>    NSView* superView = [self superview];
>    [self removeFromSuperview];
> //    [superView addSubview: self];
> }
> 
> ...then dealloc does in fact get called. But if I uncomment that one line, 
> which references self, dealloc does not get called.

I suspect it works because the ARC implementation is "suboptimal", in the sense 
that it's causing self to be autoreleased as a result of being used in a later 
expression. If the implementation ever improved to avoid using autorelease, I'd 
expect it to start crashing in this scenario.

_______________________________________________

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