[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Weak dictionaries
From: |
David Chisnall |
Subject: |
Re: Weak dictionaries |
Date: |
Thu, 14 Jun 2012 10:41:58 +0100 |
On 14 Jun 2012, at 10:23, Thomas Davie wrote:
> Thanks David,
>
> I did not know NSMapTable existed, and indeed, providing the ability to have
> ZWRs to the objects (but still copied keys) is exactly what I was after. Can
> you elaborate on exactly how Apple's semantics here are "wrong"?
In OS X (and GNUstep since we fixed that 'bug'), a map table that is supposed
to have weak values actually has unsafe unretained values. This is compatible
with the old mode before zeroing weak references were possible, but is a bit
ugly.
> Finally, the particular project I'm working on at the moment is a non-ARC
> project, can you define what "ARC mode" means in this context?
Compiling with -fobjc-arc. If you're not doing this, then it's harder. You
can use the weak read and write barrier functions in the runtime explicitly
(which is what GNUstep did), as long as you are careful to ensure that every
access goes via these.
> Do I understand you correctly in that you're simply saying that I can use
> __weak as normal to create a weak reference to the map table if the file
> containing the reference is compiled with -fobjc-arc?
No, __weak must qualify the pointer, not the
pointer-to-something-containing-the-pointer. If you did this, then the map
table itself would be weak, so would probably be freed as soon as you returned
from the function that created it, which is probably not what you want...
> Am I correct in thinking that weak referenced objects within the map table
> will still work fine even if no file in my project is compiled with ARC
> enabled?
If the map table itself is compiled with ARC and uses __weak, or if you
explicitly use objc_storeWeak() and objc_loadWeak(), then it will work
correctly even if nothing else uses ARC.
David