the method [NSArray arrayWithObjects:] returns an autoreleased object, there's no need for you to release it yourself. According to ObjC memory management guidelines you only release when you. Retain and object, init/alloc and object or copy it. In this case I think your array is getting released before you have a chance to use it in your next method.. try this:
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]; * [pnl retain]; // or use a NSArray *pnl = [[NSArray alloc] initWithObjects:] method.* [parentDocument setPlayerNameList:pnl]; [pnl release]; ------ yandy On Sat, Jan 2, 2010 at 2:44 PM, Charles Jenkins <[email protected]>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 protected]) > > 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: > http://lists.apple.com/mailman/options/cocoa-dev/yandyr%40gmail.com > > This email sent to [email protected] > _______________________________________________ Cocoa-dev mailing list ([email protected]) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [email protected]
