objective-c beginner: getter setter prob and EXC_BAD_ACCESS error

Go To StackoverFlow.com

0

Iam getting an EXC_BAD_ACCESS all the time and I cannot figure out why...

Simple task:

The Parser Class pases XML with touchXML in an NSMutableArray called listArray. In the Method grabCountry I can access the listArray and listArray.count works well.

Now I need the listArray.count in another Class the MasterViewController. But Im getting an EXC_BAD_ACCESS error all the time. Please help!

Here is the code snipplet: Parser.h

#import <Foundation/Foundation.h>

@interface Parser : NSObject

@property (strong, retain) NSMutableArray *listArray;
@property (strong, retain) NSURL *url;

-(void) grabCountry:(NSString *)xmlPath;
@end

Parser.m

#import "Parser.h"
#import "TouchXML.h"

@implementation Parser
@synthesize listArray;
@synthesize url;

-(void) grabCountry:(NSString *)xmlPath {

    // Initialize the List MutableArray that we declared in the header
    listArray = [[NSMutableArray alloc] init];  

    // Convert the supplied URL string into a usable URL object
    url = [NSURL URLWithString: xmlPath];

   //XML stuff deleted

    // Add the blogItem to the global blogEntries Array so that the view can access it.
   [listArray addObject:[xmlItem copy]];

  //works fine
  NSLog(@"Amount: %i",listArray.count);
}

 @end

MasterViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TouchXML.h"
#import "Parser.h"

@class Parser;

    @interface MasterViewController : UITableViewController{

    Parser *theParser;

}
@end

MasterViewControlelr.m

- (void)viewDidLoad
{
NSString *xmlPath = @"http://url/to/xml.xml";

theParser = [[Parser alloc] init];
//Starts the parser
[theParser grabCountry:xmlPath];

//Here I want to access the Array count, but getting an BAD ACCESS error
NSLog(@"Amount %@",[theParser.listArray count]);

[super viewDidLoad];
}

Can anyone explain me what the problem here is? Thanks!

2012-04-05 23:48
by Nico


1

Internally, each @property has a corresponding instance variable.

In your -grabCountry method, you are directly accessing the instance variable in the statement listArray = [[NSMutableArray alloc] init]; (same with url = [NSURL URLWithString: xmlPath];), instead of the @property's setter method, causing the NSMutableArray that you alloc-init'd to not be retained by the property. To invoke the @property's setter method, you should call

NSMutableArray *temp = [[NSMutableArray alloc] init];
self.listArray = temp; // or [self setListArray:temp];
[temp release];

If you want to have Xcode show an error when you are directly accessing the instance variable of an @property, you can have @synthesize listArray = _listArray, which changes the name of the instance variable to _listArray.

Generally, if there is an alloc-init, there must be a corresponding release (except if using Automatic Reference Counting).


Also, in the [listArray addObject:[xmlItem copy]]; statement, the call to copy is not needed, as NSArrays retain every object that is added to them. Calling copy also increases the retain count, which is another leak. Instead, you should just have [self.listArray addObject:xmlItem];


You are getting EXC_BAD_ACCESS because in NSLog(@"Amount %@",[theParser.listArray count]);, you are using %@ format specifier, which is for NSStrings. You want to print the array's count, an integer, so you should be using %d or %i.

2012-04-06 00:12
by neilvillareal
Thanks for the reply. I changed everything and it sounds plausible to me :) But Im getting the same error when Iam trying to access it: [theParser.listArray count] (from the MasterViewController class - Nico 2012-04-06 00:17
Finally caught the problem... You are using %@ when printing the count (an integer), when you should be using %d or %i. I also usually get caught by that. =) I have edited my answer to reflect this - neilvillareal 2012-04-06 00:31
%i works... copy & paste mistake :-/ You made my day - Nico 2012-04-06 00:33
Ads