Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSArray "EXC_BAD_ACCESS" problems

NSArray "EXC_BAD_ACCESS" problems
Thread Tools
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Jul 8, 2001, 11:29 AM
 
Something is going wrong with how I am creating some array. First of all I am trying to use the application:openFile: method to read incoming files into an NSArray. I then try and pass this array as an argument list to an NSTask. However, whenever I try to pass this array to the NSTask I get an "EXC_BAD_ACCESS" error. I tried to check the integrity of the NSArray by outputting the contents to a textview with a for loop but when I try and extract the "count" property it gives me the same error and crashes.

Here is some of the code:
- (BOOL)applicationNSApplication *)theApplication openFileNSString *)filename
{

if (!listing){
listing = (NSArray *)[NSArray arrayWithObject:filename];
}
else{
[self addToArray:filename];
}
return YES;
}

- (void)addToArrayNSString *)filename
{
listing = (NSArray *)[listing arrayByAddingObject:filename];
}

...and trying to extract data later:
for (county=1; county <[listing count]; county++)
{
[contents setString:[[contents string] stringByAppendingString:[listing objectAtIndex:county]]];
}


This has me completely stumped so can someone help me out?

Thanks,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
Jul 8, 2001, 12:07 PM
 
Since listing is an instance variable, you need to "own" a reference to it. [NSArray arrayWithObject:obj] returns an autoreleased array, so by the time the next user event happens, it has been freed. The errors you're seeing happen when you try to send messages to a freed object.

Either add a -retain call ([[NSArray arrayWithObject:obj] retain]), or use the alloc/init version, which has an implicit retain ([[NSArray alloc] initWithObject:obj]).

You'll probably also find it easier to use an NSMutableArray. If contents is an NSMutableString, you can just append the string to it as well...

- (BOOL)applicationNSApplication *)theApplication openFileNSString *)filename
{
[self addToArray:filename];
}

- (void)addToArrayNSString *)filename
{
if (listing == nil)
listing = [[NSMutableArray alloc] initWithObject:filename];
else[listing addObject:filename];
}

...and trying to extract data later:
for (county=1; county <[listing count]; county++)
{
// Assuming contents is an NSMutableString here
// also, you're starting at index 1, this will skip the
// first entry in the listing array here (which is index 0).
[contents appendString:[listing objectAtIndex:county]]];
}
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Jul 8, 2001, 12:11 PM
 
You're using a convenience constructor, which returns and -autorelease'd object. You need to retain it. Also, you're not freeing memory correctly later on. OK, looks like lindberg replied to this between me loading the page and hitting the reply button
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2