FROM : Alastair Houghton
DATE : Tue Feb 19 20:28:33 2008
On 19 Feb 2008, at 19:03, Michael Ash wrote:
> On Feb 19, 2008 1:36 PM, Quincey Morris
> <<email_removed>> wrote:
>> Furthermore, your example made me wonder if there's another
>> optimization hole, that has nothing to do with interior pointers, but
>> which also reflects a variable lifetime indeterminacy:
>>
>> __weak NSString* string1 = [@"string1" copy];
>> NSString* string2 = @"string2";
>> [[NSGarbageCollector defaultCollector] collectExhaustively];
>>
>> Couldn't this result in the garbage collector zeroing 'string2' under
>> unlucky (but common) optimization conditions?
>
> All local variables (variables stored on the stack and in registers)
> are strong. Even the ones marked __weak. __weak (and __strong) only
> apply for variables stored on the heap.
...and globals...
Anyway, the point is not to read too much into __strong or __weak.
They do what the documentation says, no more, no less. It isn't a
good idea to invent all sorts of theories on the basis of "__strong
types" etcetera, because that isn't (presently) how things work.
There's nothing wrong with pointer arithmetic on pointers returned by
NSAllocateCollectable(), it's just that you have to be careful not to
dispose of the pointer to the start of the block before you're done
with it. Sadly, right now, that requires an awareness of what the
optimiser might do to your code.
Kind regards,
Alastair.
--
http://alastairs-place.net
DATE : Tue Feb 19 20:28:33 2008
On 19 Feb 2008, at 19:03, Michael Ash wrote:
> On Feb 19, 2008 1:36 PM, Quincey Morris
> <<email_removed>> wrote:
>> Furthermore, your example made me wonder if there's another
>> optimization hole, that has nothing to do with interior pointers, but
>> which also reflects a variable lifetime indeterminacy:
>>
>> __weak NSString* string1 = [@"string1" copy];
>> NSString* string2 = @"string2";
>> [[NSGarbageCollector defaultCollector] collectExhaustively];
>>
>> Couldn't this result in the garbage collector zeroing 'string2' under
>> unlucky (but common) optimization conditions?
>
> All local variables (variables stored on the stack and in registers)
> are strong. Even the ones marked __weak. __weak (and __strong) only
> apply for variables stored on the heap.
...and globals...
Anyway, the point is not to read too much into __strong or __weak.
They do what the documentation says, no more, no less. It isn't a
good idea to invent all sorts of theories on the basis of "__strong
types" etcetera, because that isn't (presently) how things work.
There's nothing wrong with pointer arithmetic on pointers returned by
NSAllocateCollectable(), it's just that you have to be careful not to
dispose of the pointer to the start of the block before you're done
with it. Sadly, right now, that requires an awareness of what the
optimiser might do to your code.
Kind regards,
Alastair.
--
http://alastairs-place.net






Cocoa mail archive

