• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why is [NSArray arrayWithObjects:] failing for me?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is [NSArray arrayWithObjects:] failing for me?


  • Subject: Re: Why is [NSArray arrayWithObjects:] failing for me?
  • From: Yandy Ramirez <email@hidden>
  • Date: Sun, 3 Jan 2010 00:10:24 -0500

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@hidden>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

  • Follow-Ups:
    • Re: Why is [NSArray arrayWithObjects:] failing for me?
      • From: Roland King <email@hidden>
References: 
 >Why is [NSArray arrayWithObjects:] failing for me? (From: Charles Jenkins <email@hidden>)

  • Prev by Date: Re: Why is [NSArray arrayWithObjects:] failing for me?
  • Next by Date: Re: Why is [NSArray arrayWithObjects:] failing for me?
  • Previous by thread: Re: Why is [NSArray arrayWithObjects:] failing for me?
  • Next by thread: Re: Why is [NSArray arrayWithObjects:] failing for me?
  • Index(es):
    • Date
    • Thread