views:

24

answers:

2

Hello,

I've begun work on a side project, so the codebase is very small, very little that could go wrong. Something strange is happening. In viewDidLoad I initialise an array set as a property:

@property (nonatomic, retain) NSMutableArray * story_array;

And fill it with data. This printout is fine:

NSLog(@"%@", ((ArticlePreview *)[self.story_array objectAtIndex:0]).article);

I have a gesture recognizer:

UITapGestureRecognizer * openStory = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showStory)];

Tapping on it calls a method whose first line is this (i.e. the same NSLog):

NSLog(@"%@", ((ArticlePreview *)[self.story_array objectAtIndex:0]).article);

But this causes a bad access. Accessing story_array itself is fine (it'll say it has however many ArticlePreview objects inside) but accessing their fields is a no-no.

The story_array is init'ed as follows:

self.story_array = [[NSMutableArray alloc] init];

A: 

It would appear that either the ArticlePreview instance or the article it contains is deallocated prior to the call to NSLog, though without seeing the code you're using to populate the array it's impossible to be sure which one.

In general with memory management issues like this, posting isolated fragments of code doesn't give a full enough picture to allow people to spot the problem.

jlehr
A: 

Assignment to the fields of the ARticle Preview object were not done properly. I had:

someField = someValue;

I needed:

self.someField = someValue;

I still find that a bit crazy, but there you go. Solved.

mtc06