Re: Changing Subview Order Prevents Dragging
Re: Changing Subview Order Prevents Dragging
- Subject: Re: Changing Subview Order Prevents Dragging
- From: Quincey Morris <email@hidden>
- Date: Mon, 13 May 2013 22:09:02 -0700
On May 13, 2013, at 21:30 , Andy Lee <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden