discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Infinite loop in objc_storeWeak


From: Thomas Davie
Subject: Re: Infinite loop in objc_storeWeak
Date: Mon, 18 Jun 2012 12:02:58 +0100

On 18 Jun 2012, at 11:51, David Chisnall wrote:

> This code works as expected.  It appears that you are misunderstanding how 
> weak variables work in Objective-C.
> 
> When you read from a __weak pointer, you get an autoreleased pointer.  This 
> guarantees that the weak pointer is not deallocated at the wrong time.  For 
> example, in your example you do:
> 
> [r target];
> 
> This loads the __weak ivar _target, retains and autoreleases it, and then 
> returns it.
> 
> When called in ARC mode, the compiler inserts a call to 
> objc_retainAutoreleasedReturnValue(), which pops the object off the 
> autorelease pool and retains it.  This is then followed by a call to 
> objc_release(), which decrements the reference count so that there are now no 
> strong pointers to the weak object, and once you do o = nil; (which becomes a 
> call to objc_storeStrong(), or possibly objc_release() followed by an 
> assignment )it can be deallocated.  
> 
> In ARC mode, the compiler can be a lot more aggressive about removing things 
> from the autorelease pool, which is one of the main reasons that it leads to 
> increased performance: objects don't hang around in autorelease pools for as 
> long a time burning data cache.  
> 
> Short version: never release a __weak pointer.  Bad things will happen.  As I 
> said, the most likely cause for the error that you saw is some incorrect 
> memory management in your code.

David,

I get that that weak pointer shouldn't be released, I added that line in just 
to check that it wasn't zeroed if messages were sent to it.  The bug I'm 
referring to is the fact that the second log line prints out a pointer value 
and description, despite the only strong pointer to o having been released.  By 
my understanding, [r target] should result in nil on line 12.

Thanks

Tom Davie


reply via email to

[Prev in Thread] Current Thread [Next in Thread]