Re: Initializing an Array
Re: Initializing an Array
- Subject: Re: Initializing an Array
- From: Marcel Weiher <email@hidden>
- Date: Tue, 22 Jan 2002 13:17:26 +0100
On Tuesday, January 22, 2002, at 10:58 , Jeff Galyan wrote:
Also, this code:
NSArray *myArray = [[NSArray arrayWithObjects:@"Do
unto",@"others", nil] retain];
Be careful where you put your retain calls. I've got some code I'm
debugging
right now where I did exactly like the above, but it turns out the
message
is getting sent to a different instance than I expected,
That should not be the case. Be careful with debugging optimized code!
Although the optmization will yield the same end-result, intermediate
values will have been re-arranged quite signicantly, with variables
disappearing, taking on different values etc.
or rather, the
return type indicated in the docs apparently has no meaning to the
runtime.
For example, one would think that because retain returns type id that
that
means you can keep that pointer around. Wrong. The runtime is
deallocating
all of those pointers I set up like above almost as soon as they're
returned
from the receiver (stepping through in the debugger, I saw one of them
last
for two statements before it evaporated... "0x0" is never an address one
likes to see appear in the vars pane).
This is a very typical sign of debugging optimized code.
So, you have to do this:
NSArray *myArray = [NSArray arrayWithObjects:@"Do unto", @"others",
nil];
[myArray retain];
Thist code has the same effect as the above. However, it may force the
optimizer to actually keep myArray arround in its expected location a
bit longer. Message sends and non-static function calls are opaque to
the optmizer, so you can use them to make variables 'sticky'.
Makes me wonder why retain returns anything at all.
-retain is fine, I am almost 100% certain that this is something else.
Marcel
--
Marcel Weiher Metaobject Software Technologies
email@hidden www.metaobject.com
Metaprogramming for the Graphic Arts. HOM, IDEAs, MetaAd etc.