1
votes

I have a NSMutableArray property declared as (nonatomic, retain) called Categories. In dealloc, I release the memory for that variable. Originally, in viewDidLoad, I allocated that array and called another method that populated that data with dummy data. Basically

[categories addObject:someObject1];
[categories addObject:someObject2];
....

This was working.

Then I got the real data from a coworker in a method that talked to the model and returned an autoreleased array. I was getting EXC_BAD_ACCESS after I used the method.

categories = [datamanager GetCategories];

Now is that because I didn't retain the autoreleased array returned from the datamanager GetCategories method?

If so, I need to delete the alloc/init in viewDidLoad since that would be a memory leak right?

Thanks, just trying to make sure I'm understanding memory mgmt correctly.

1

1 Answers

3
votes

I think you're on the right track. If you're getting categories from a method that returns an autoreleased object then you want to retain that object. So you can change your assignment above to either of the following:

// use the synthesized setter's retain
self.categories = [datamanager GetCategories];

// or explicitly retain the object (which clearly indicates the code's intention)
categories = [[datamanager GetCategories] retain];

The alloc/init in viewDidLoad is unnecessary.