Re: Why is [NSArray arrayWithObjects:] failing for me?
Re: Why is [NSArray arrayWithObjects:] failing for me?
- Subject: Re: Why is [NSArray arrayWithObjects:] failing for me?
- From: Roland King <email@hidden>
- Date: Sun, 3 Jan 2010 13:10:05 +0800
you should go back and read the memory management documentation again until it's burned into your memory.
arrayWithObjects returns an array you do not own and thus do not need to release (and must not).
On 03-Jan-2010, at 3:44 AM, Charles Jenkins wrote:
> Hello, everyone. I'm struggling through the steep Cocoa learning curve, and even things that should seemingly be very easy turn out to be difficult for me.
>
> I have an NSView in which I ask for the player names for a 4-person game. I have hooked the NSTextField objects to IBOutlet NSTextField* data members of my view's class, and I am at the point where the user clicks 'OK' and I need to save the player names.
>
> The NSTextField* variables are called playerA ... playerC, and i have no problem with using [playerA stringValue] to get the name of a player. The problem comes when I try to save the player name into an array. Here is the current version of my code:
>
> NSString* pa = [playerA stringValue];
> NSString* pb = [playerB stringValue];
> NSString* pc = [playerC stringValue];
> NSString* pd = [playerD stringValue];
> NSArray* pnl = [NSArray arrayWithObjects:pa,pb,pc,pd,nil];
> [parentDocument setPlayerNameList:pnl];
> [pnl release];
>
> I think adding the strings to an array will retain them; that's why there aren't a bunch of retain calls here. setPlayerNameList should retain as well, so the only memory management I think I have to do here is release pnl once.
>
> pa, pb, pc, and pd are all good, according to the debugger as I step through, so the call to arrayWithObjects: looks good. But when I get the resulting array, all of the objects are trash and I get a BAD ACCESS exception when anybody tries to use them.
>
> I'm using XCode 3.2.1 on Snow Leopard, but I'm building using the 10.5 or later SDK, because I'm not using an 10.6-only features.
>
> So, what the heck am I doing wrong? Thanks!
> _______________________________________________
>
> 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