Sjakelien
Sjakelien

Reputation:

iPhone Memory Management

I'm about to make a fool out of myself, but I've noticed many friendly and patient people around here, so I just give it a try:

I'm developing an iPhone app, that contains a database of car reviews. I want the user to be able to share the reviews by email. So, when he/she finds an interesting car in the app, he/she would hit a button, and the app composes an email through the iPhone's Mail.app.

Now. I'm a newbie, and I have to admit that I'm not too familiar with memory management on the iPhone. The code that I wrote, this specific mail method, exits the application with the scary "Program received signal: “EXC_BAD_ACCESS”" message. A bit of Googling suggest that this is the result of bad memory management.

With my little understanding of this matter, I started explicitly initializing and afterwards releasing all temporary variables, like a madman. Nevertheless, the “EXC_BAD_ACCESS” keeps on showing up.

Interesting point here: as soon as I kill my app, the constructed URL still triggers Mail.app, and happily creates the email for me.

Please consider the following sample code, and shoot me.

- (IBAction) sendCartoFriend
{
    CarAppDelegate *appDelegate = (CarAppDelegate *)[[UIApplication sharedApplication] delegate];

    //Read the html template
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString *emailFile = [resourcePath stringByAppendingPathComponent:@"MailDummy.html"];
    NSMutableString *eMailRaw = [[[NSMutableString alloc] initWithContentsOfFile:emailFile]autorelease];

    //set the variables
    NSString *carNamePlaceholder = [[NSString alloc] initWithString:@"CarTitle"];
    NSString *carName = [[NSString alloc] initWithString:car.shortname];
    [eMailRaw replaceOccurrencesOfString:carNamePlaceholder withString:carName options:NSCaseInsensitiveSearch range:NSMakeRange(0, [eMailRaw length])];
    [carNamePlaceholder release];
    [carName release];

    NSString *carReviewPlaceholder = [[NSString alloc] initWithString:@"CarReview"];
    NSString *carReview = [[NSString alloc] initWithString:car.review];
    [eMailRaw replaceOccurrencesOfString:carReviewPlaceholder withString:carReview options:NSCaseInsensitiveSearch range:NSMakeRange(0, [eMailRaw length])];
    [carReviewPlaceholder release];
    [carReview release];

    //there are 5 more of these find/replace actions. the "CarReview" though is the biggest. It might contain several hundred of characters.

    //compose the message
    NSString *eMailSubject = @"Nice little car!";
    NSString *encodedSubject = [[NSString alloc] initWithString:[eMailSubject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    NSString *eMailBody = eMailRaw;
    NSLog(eMailBody);
    NSString *encodedBody = [[NSString alloc] initWithString:[eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


    NSString *urlString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"mailto:?subject=%@&body=%@", encodedSubject, encodedBody]];
    NSURL *url = [[NSURL alloc] initWithString:urlString];

    [urlString release];
    [encodedBody release];
    [encodedSubject release];
    [eMailRaw release];

    [[UIApplication sharedApplication] openURL:url];

    [url release];
    }

Upvotes: 0

Views: 1144

Answers (1)

rein
rein

Reputation: 33465

Hmm.. at first glance:

you're releasing eMailRow even though it's set to autorelease.

Could this be the problem?

Upvotes: 3

Related Questions