Re: Trying to understand -- please help...
Re: Trying to understand -- please help...
- Subject: Re: Trying to understand -- please help...
- From: "Paul Bailey" <email@hidden>
- Date: Wed, 21 May 2008 17:20:32 +0100
What you're doing here is allocating an empty array, and then setting the
pointer of your instance variable to a new array, created with the
arrayWithObjects message. An array returned by this message is
autoreleased, so at the end of your init method, it's being released. All
these type of convenience methods autorelease the object they return, so to
have it hang around, you'll have to retain it.
Also you're leaking the memory from the first alloc init, as this is
allocated, but the reference to it is lost.
To fix this, you can remove your initial alloc of an array, and just send a
retain message to the array that you populate with the cities. That way, it
won't disappear without your consent (i.e. when you elect to send it a
release message).
Cheers,
Paul
On Wed, May 21, 2008 at 5:05 PM, john darnell <email@hidden>
wrote:
> Hello Everyone:
>
> I decided that it was time for a play period this morning so I set a
> task for myself; to place a combo box on a dialog and
> populate it with the names of ten cities--no more than that. I just
> wanted to see if my understanding of the language had
> progressed enough to do so.
>
> I built a dialog in InterfaceBuilder with three objects on it: ComboBox,
> and two buttons, one set to load the cities into the
> box, and one to set the default. MY header looked like this:
>
>
> #import <Cocoa/Cocoa.h>
>
> @interface Controller : NSObject
> {
> IBOutlet NSComboBox *cityComboBox;
> NSArray *cityArray;
> }
>
> - (IBAction) loadCities:(id)sender;
> - (IBAction) setDefault:(id)sender;
>
> @end
>
> and my implementation looked like this:
>
> #import "Controller.h"
>
> @implementation Controller
> - (id) init
> {
> cityArray = [[NSArray alloc] init];
> NSString *c0 = @"New York"; //Ten NSString objects created here
> ...
> NSString *c9 = @"Virginia Beach";
> cityArray = [NSArray arrayWithObjects: c0, ...c9, nil];
> return self;
> }
>
> - (IBAction)loadCities:(id)sender
> {
> [cityComboBox addItemsWithObjectValues:cityArray];
> }
>
> - (IBAction)setDefault:(id)sender
> {
> [cityComboBox selectItemAtIndex: 6];
> [cityComboBox setObjectValue:[cityComboBox
> objectValueOfSelectedItem]];
> }
>
> @end
>
> The program compiled and linked without a problem, but when I ran it, I
> got a couple of errors, both related to the fact that cityArray was
> empty.
> When I stepped through the code, I found that cityArray was indeed
> getting filled in the init call, but by the time I got around to the
> loadCities call, cityArray
> was empty.
>
> Moving the cityArray population to the loadCities call fixed the
> problem, but I don't understand why cityArray was trashed when it
> was a member of the object in the first place.
>
> I am obviously trying to think of Objective C in terms of C++. Where
> did my thinking go wrong?
>
> R,
> John A.M. Darnell
> Team Leader
> Walsworth Publishing Company
> Brookfield, MO
> John may also be reached at email@hidden
>
> Trivia Question: In SciFi Channel's hit series, FARSCAPE, who played
> the voice of Pilot?
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden