views:

51

answers:

2

I'm using openURL to send an email w/some links. The function looks like this:

//
+ (void) sendEmail:(NSString *) subject withBody:(NSString *)body {
 NSString *mailString = [NSString stringWithFormat:@"mailto:?@&subject=%@&body=%@",
       [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
       [body  stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
 [mailString autorelease];
 }

The code that calls this function looks like this:

[AppNameAppDelegate sendEmail:subjectLine withBody:bodyText];

No matter what the subjectLine and bodyText, I get an error like this:

Program received signal:  “EXC_BAD_ACCESS”.

The email actually pops up, so I know it's making it through that openURL. But by then I guess the program has crashed and so it is not restored when you exit the email pane.

Any ideas on why this is happening?

Issue looks like this but that answer isn't applicable. It looks more like this forum post but unfortunately that issue looks like it was never solved.

UPDATE: When removing the autoreleased mailString (per the instructions in the StackOverflow ticket mentioned above) it does not improve the situation.

//
+ (void) sendEmail:(NSString *) subject withBody:(NSString *)body {
 NSString *mailString = [NSString stringWithFormat:@"mailto:?@&subject=%@&body=%@",
       [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
       [body  stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
 }

Setting a malloc_error_break breakpoint does not seem to do anything. The output still looks like this:

AppName(1424,0x3e9097c8) malloc: *** error for object 0x16fdf0: double free
*** set a breakpoint in malloc_error_break to debug
+3  A: 
NSString *mailString = [NSString stringWithFormat:...

already returns autoreleased string - you must not sent any autorelease messages to it

Vladimir
Thank you for your answer. Unfortunately, I had tried this and it does not seem to solve my problem. Pls see updated question for output.
editor
What system do you target? I've made a sample - code without extra autorelease seems to work fine on iPhoneOS 3.1... May be try to clean and rebuild your project? Or your real problem may be not in this function but somewhere else.
Vladimir
I'm targeting 4.0. Unfortunately I guess the issue goes deeper than I can expect to find an answer for.
editor
Try to review your code - may be there's more redundant (auto)releases... Especially in the functions that get called when application goes to the background
Vladimir
+1  A: 

Use NSZombieEnabled to debug as I described in this answer.

jtbandes
Thanks for the suggestion. Will work on this. But removing this code removes the error, suggesting to me that I can bet that the cause is somewhere in these few lines of code. Will verify using this technique.
editor
Found my zombie. Any tips on how I figure out what that memory allocation represents?2010-08-22 09:34:58.165 AppName[1448:307] *** -[CFString release]: message sent to deallocated instance 0x19a0f0
editor
@editor: That means you're over-releasing a string somewhere. Check to make sure you're not making the same mistake anywhere else.
jtbandes
@editor: With that redundant `autorelease` line now removed or not?
Georg Fritzsche
The redundant autorelease is removed. In debugger, that memory assignment is filed under the variable $r10 without much more information.
editor
Look in instruments and see who is calling retain and release on the object. You don't care about the assignment, you need to figure out where your extra release is coming from.
bbum
@editor: Take a look that the strings 'subject' and/or 'body' are not being over-released outside of this method. Put a breakpoint on this method and then in the XCode debugger look at the variables pane (upper right) and check the addresses of 'subject' and 'body'. Then see if either of those addresses match the zombie.
Firoze Lafeer