RE: basic Obj-C lesson (was runtime error with NSArray no more!)
RE: basic Obj-C lesson (was runtime error with NSArray no more!)
- Subject: RE: basic Obj-C lesson (was runtime error with NSArray no more!)
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Fri, 6 Dec 2002 14:45:36 -0500
>
On Friday, December 6, 2002, at 10:18 AM, Sherm Pendley wrote:
>
> Is that the array you created with arrayWithObjects:? If so, that
>
> method returns an autoreleased object. An autoreleased object normally
>
> only exists for the lifetime of the event (in this case, the
>
> awakeFromNib: event) in which it was created.
>
> <snip>
>
>
>
so are you saying that on my first iteration I should add something
>
like [[myArray objectAtIndex: i] retain]; and then of course release it
>
after the second iteration?
>
No. I think the autoreleased object Sherm referred to is the array, not the
member of the array. If the array goes bye-bye and you try to access it the
next time someone clicks on a button, your app will crash. So retain the
array. Either
(a) create it with one of the arrayWith... methods (which are class methods,
which create autoreleased objects) and explicitly retain it, or
(b) create it with an alloc/init... method.
And, of course, assign it to a variable that has the right scope (perhaps an
instance variable).
>
that makes a good deal of sense. man, I'm used to C and C++ I don't
>
know how long it will take to get used to this memory management
>
scheme.
Search the archives for the many resources on this issue.
>
In this specific instance I'm not sure I understand how the
>
NSButton object is created and destroyed.
The nib creates it. You are adding it to an array. Two different things.
Incidentally, when you add the button to the array, the array automatically
retains the button. The array (assuming it's immutable) won't release the
object until the array is deallocated. What this means is that even if the
nib were to release the button, the button would still exist as a member of
the array until the array was deallocated.
>
I mean its in my UI but
>
you're saying that that the object is only retained as long as the call
>
is going on?
>
Shouldn't the pointer in my NSArray still point to the
>
object that exists in the UI? I would love to hear the answer to this,
>
thanks.
The button is still there. The array may not be.
Jonathan
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.