Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Denvildaste

macrumors newbie
Original poster
Apr 7, 2010
8
0
Hello guys, I'm new to Objective-C programming, I've read about memory management and things has been working well for me until I ran into this error:

EXC_BAD_ACCESS

Here's what I did:

1. I created a class to serve as a data object (like a bean in Java) called ClassA.

2. Inside ClassB(which is a view controller) I have a method where I create an instance of ClassA and fill it with data, then I create an instance of ClassC (also a view controller that has a global variable of type ClassA with property retain) and pass my instance of ClassA to it and then push it on top of my navigation stack, the method ends here.

3. Using the instance of ClassA inside ClassC only works in viewDidLoad, if I try it in any other place inside ClassC (like from inside a function) I get the error above!

Now from my understanding, the class should stay in memory until ClassC is destroyed, so I don't really understand what I'm doing wrong, any help is appreciated.
 
Class B

Code:
-(IBAction) buttonPressed: (id) sender {
	// Start an operation
	NSOperationQueue *queue = [NSOperationQueue new];
	NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getRequestResult) object:nil];
	[queue addOperation:operation];
	[operation release];
}


-(void) getRequestResult {
         // ... (Some code omitted from here)
	NSXMLParser *parser = [[ NSClassFromString(@"NSXMLParser") alloc] initWithData:data];
	
        // inside initClassA I initialize a mutable array
	ClassA* classA = [[ClassA alloc] initClassA];
	
	if (parser != nil) {
		[parser setDelegate:classA];
		[parser parse];
	}
	
	[parser release];
	
	ClassC* classC = [[ClassC alloc] initObject:classA];
	
	[self.nav pushViewController:classC animated:YES];
	
	[classC release];
	//[classA release]; // releasing here causes an error
}
Class C Header

Code:
@interface ClassC : UIViewController<UITableViewDelegate, UITableViewDataSource> {
	ClassA *classA;
}

@property (nonatomic, retain) ClassA *classA;

-(ClassC*)initObject:(ClassA*)classA;

Class C Body

Code:
@synthesize classA;

- (void)viewDidLoad {
	self.items = self.classA.items; // A work around I found, store items array in a global array before classA stops giving me access
    [super viewDidLoad];
}

-(ClassC*)initObject:(ClassA*)classA {
	self = [super init];
	[COLOR="Red"]self.classA = classA;[/COLOR] // This line doesn't increase the retain count of ClassA, I printed the retainCount before this line and after it, it was 1 in both cases 
	return self;
}
 
Is ClassA an autoreleased object?
And why are you doing this
Code:
[[ NSClassFromString(@"NSXMLParser") alloc]
when you can do this
Code:
 [NSXMLParser alloc]
 
Is ClassA an autoreleased object?
And why are you doing this
Code:
[[ NSClassFromString(@"NSXMLParser") alloc]
when you can do this
Code:
 [NSXMLParser alloc]


Thanks about the XMLParser info, I actually copy pasted that code bit and didn't inspect it enough.

As for Class A, no it's not an auto release as far as I know.

I'm going to edit my second post and highlight a weird thing that happens in Class C, it might give you a clue.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.