views:

18

answers:

1

I'm sharing my string over the AppDelegate-Class:

SpeakersAppDelegate *mainDelegate = (SpeakersAppDelegate *)[[UIApplication sharedApplication] delegate];
[mainDelegate setShareText:xmlString]; 

And get the string back from the AppDelegate-Class:

 SpeakersAppDelegate *mainDelegate = (SpeakersAppDelegate *)[[UIApplication sharedApplication] delegate];
 NSString *xmlString = [mainDelegate getShareText];

With no problems if I use:

xmlString = @"<rsp><photos><photo url='xyz.jpg' thumb='xyz.jpg' /></photos></rsp>";

But the app crashes when I use (xmlString get's the content of the url correctly, the problem is the resiving of the string from AppDelegate-Class when xmlString is filled withContentFromURL):

xmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];

Does anyone knows a solution? Thanks for help!

+1  A: 

stringWithContentsOfURL: returns an auto-released string. So when the autorelease pool is drained (usually when it's done processing the current event), your string is released. You then try to access it, and boom.

I suspect that your delegate is not managing the lifecycle of xmlString. You probably want setShareText: to retain, or copy, the string. Be careful to release any previous string.

Jon Reid
but why it is working then if I set the string in code? setShareText is working! NSLog shows that the value is set correctly
pbcoder
@pbcoder: It doesn't matter if the value is set correctly. What matters is if the object storing the value owns it. Your setter should retain the argument, but it looks like it's not doing that.
Chuck
You mean xmlString = [[NSString alloc] init]?
pbcoder
@pbcoder, it works with the string in code because NSString literals disregard retain/release. When you call `stringWithContentsOfURL:` (which is deprecated, incidentally), you are calling a convenience method that returns an auto-released object. It actually invokes a `initWithContentsOfURL:`
Jon Reid