1

My question is very similar to this question but with a few differences.

I have a class that takes in XML int the form of an NSString, parses out some objects and returns them in an NSArray somewhat like this:

//Parser.m
+(NSArray *)parseMessagesFromXml:(NSString *)xml
{
   NSMutableArray *messages = [[NSMutableArray alloc] init];

   //do some work parsing the xml
   //for each <row>
   //   Message *m = makeMessageFromRow(row);

   return [messages autorelease];
}

Then in my view controller class I declare an NSArray:

//MyViewController.h
NSArray *messages;
@property (nonatomic, retain) NSArray *messages;

and assign it using the above method:

//MyViewController.m
messages = [Parser parseMessageFromXml:xml];
[[self tableView] reloadData];

So here comes the problem: when i assign the array to messages it has elements in it, but they are all "out of scope." I have debugged the problem and I know that the parsing method is correctly creating the objects and adding them to the NSMutableArray before returning it. When I try to access the objects in messages my app crashes and the debugger says EXC_BAD_ACCESS. What is more peculiar is this: if i store the array into a local variable it works just fine:

NSArray *temp = [Parser parseMessageFromXml:xml]; 
//temp has all the right objects and they are in scope

messages = temp;
//messages has the objects, but cannot access them (they are out of scope).

It is as if I can legally view them in a local scope, but i cannot assign them to a member variable. I have even tried iterating over the returned array, adding each one to messages individually, but the result is the same: they are out of scope. I am totally clueless on this one.

What is it about messages as a member variable that doesn't allow it to hold these objects?

1 Answer 1

2

The problem is that the array is being released. When you call autorelease in parseMessagesFromXml:, you tell the array that it should be released sometime in the future. This is happening before the table reloads its data. You need to retain the array again to prevent it from being released. In this case, it is as simple as using the accessor methods to set your property instead of setting the instance variable directly.

//MyViewController.m
self.messages = [Parser parseMessageFromXml:xml];
[[self tableView] reloadData];
Sign up to request clarification or add additional context in comments.

5 Comments

Conceptually speaking, your view controller should "take ownership" of the results of parseMessageFromXml.
that is exactly what I'm doing. Also you will notice that messages uses the retain property, so it isn't getting deallocated. And on top of that, I have carefully debugged the problem and I know that it is not getting deallocated prematurely. Look that he 4th code example: in the temp NSArray the objects are still in scope while at the same time they are out of scope in messages. Same objects, same time, but for one they are visible and the other they are not.
@mtmurdock You're assigning the array to the instance variable (messages = [Parser parseMessageFromXml:xml];) instead of using the property as suggested by ughoavgfhw (self.messages = [Parser parseMessageFromXml:xml];).
so what you are saying is that self.messages and messages is different? That seems like a flaw in the language if i can access the same variable two different ways with different results.
self.messages is a @property, messages is an ivar. self.messages = ... is really syntactic sugar for [self setMessages:...]. The synthesized method setMessages: will retain the value that gets passed in, before assigning to messages (the ivar). It's not a flaw in the language...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.