-1

I have an array that I want to populate with a dictionary, however I get EXC_BAD_ACCESS when I try to view the pickerView that is populated with the array. One of these 3 lines of code causes it.

paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) copy];
  NSString *documentsDirectory = [paths objectAtIndex:0]; 
  NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"fullArray.plist"];

Full Code

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"fullArray.plist"];

dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
array = [dictionary allKeys];

[pickerView selectRow:0 inComponent:0 animated:YES];
[pickerView reloadAllComponents];

}

  • Please post the code which uses the arrays. This code tells us nothing. Some of these are not retained by default, but that might or might not matter depending on how you're using them. Also, what do you see in the debugger when the crash occur? – user1118321 Mar 16 '12 at 18:17
  • "One of these three lines" tells us nothing. Are you using ARC? – Richard J. Ross III Mar 16 '12 at 18:21
  • No, I disabled it because it interfered with other sections of my code. – user1222053 Mar 16 '12 at 18:23
  • NSZombies tells me, An Objective-C message was sent to a deallocated object (zombie) at address: 0x6d52c50. – user1222053 Mar 16 '12 at 18:23
  • It says it's been released, but where? – user1222053 Mar 16 '12 at 18:24
  • Which array does the pickerView get its data from? "array"? If so, it's not retained, which could be a problem. As for the zombie, does that address match any of the objects in this method or the method which returns data to the pickerView? – user1118321 Mar 16 '12 at 18:25
  • Find out which line exacty causes the EXC_BAD_ACCESS. The Zombie should give you a hint of what data type/class name your released object is. – Hermann Klecker Mar 16 '12 at 18:31

2 Answers2

0

Maybe the NSArray returned by NSSearchPathForDirectoriesInDomains is becoming a Zombie. You could try getting a copy of it like this:

NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) copy];
CJ Foley
  • 101
  • 4
  • Hang on, no it's not sorry, it's happening again. Instruments says it's the Array though... – user1222053 Mar 16 '12 at 18:42
  • Bah! Did it work at least once? Is this an intermittent problem? – CJ Foley Mar 16 '12 at 18:47
  • How would I write that line so it was retained? – user1222053 Mar 16 '12 at 19:00
  • `NSArray *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) retain];` – CJ Foley Mar 16 '12 at 19:10
  • 2
    You know you can disable ARC on a per-file basis by adding a compiler flag to each compilable source [like this](http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project). Maybe using ARC for most of your project (and disabling only the files that are being interfered with) would help? – CJ Foley Mar 16 '12 at 19:12
  • I've reenabled ARC, and added the retained code but it still crashes. – user1222053 Mar 16 '12 at 19:28
  • # Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller 0 0x6d51810 __NSArrayI Malloc 1 00:18.271.594 48 QuickConfig -[SecondViewController viewDidLoad] 1 0x6d51810 __NSArrayI Autorelease 00:18.271.654 0 QuickConfig -[SecondViewController viewDidLoad] 2 0x6d51810 __NSArrayI Release 0 00:18.714.209 0 Foundation -[NSAutoreleasePool release] 3 0x6d51810 __NSArrayI Zombie -1 00:18.947.685 0 QuickConfig -[SecondViewController pickerView:titleForRow:forComponent:] – user1222053 Mar 16 '12 at 19:28
  • The issue is in the pickerView:titleForRow:forComponent. – user1222053 Mar 16 '12 at 19:33
0

Looks like, with this line, you're trying to save a value into an ivar named "arrays":

array = [dictionary allKeys];

But that's not going to work (unless it's and ARC project, which you say it isn't).

You need to retain that value. Make array a property that's retained (or copied). Or (not quite as good) retain it yourself:

[array autorelease];  // Don't skip this, or it may leak.
array = [[dictionary allKeys] retain];
Dave Batton
  • 8,795
  • 1
  • 46
  • 50