0

Scenario, For the sake of DRY, I created a view controller(e.g. GeneralDelegateForEmailAndSMS) implementing the methods of protocol MFMailComposeViewControllerDelegate.

// GeneralDelegateForEmailAndSMS.h
#import <MessageUI/MessageUI.h>
@interface GeneralDelegateForEmailAndSMS : UIViewController <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@end

// GeneralDelegateForEmailAndSMS.m
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ ... }

Then inside a class method(e.g. composeDiscountInfo ) of the class being responsible for the mailing(e.g. MyMailer)

+(void) composeDiscountInfo:(ResInfoData *)resturantInfo{
    // XmsMFMailComposeViewController, a subclass of MFMailComposeViewController in order to overwrite the default auto rotation.
    XmsMFMailComposeViewController *picker = [[XmsMFMailComposeViewController alloc] init];  

    // Q: method returneds an Object with a +1 retain count.
    picker.mailComposeDelegate = [[GeneralDelegateForEmailAndSMS alloc] init]; 

    // Q: object leaked, allocated object is not referenced later in the execution path and has a retain count of +1  A:
    [picker setSubject:@"title"]; 

    // ... message body construction...  and present via AppDelegate.navController
}

My question is how to resolve the potential memory leak(mentioned above in code comments)? I can't set the delegate to auto release as it looks like will be garbage collected and crash the app. Also, the MyMailer is supposed to include only class method, there is not life cycle methods to handling the object. What's the solution?

I know very little about the profile instructment, any hint on how to use it to detect leak?

Updated:

MyMailer is supposed to be 'class methods only' class, in many view controllers that need to compose/send in-app email/SMS, I can just write code like "[MyMailer composeMailWith:data]" with implementation code listed below(i.e. composeDiscountInfo:data in the 2nd code snippet).

Inside these class methods, I wish I can set MFMailCompseViewController's delegate property(mailComposeDelegate) to a standalone class that implements MFMailComposeViewControllerDelegate.

Maybe I shall make Mailer a singleton, calling will look like '[MyMailer instance] composeThatMailWith:data' in many of VCs. I will try tomorrow!

user746403
  • 73
  • 1
  • 5
  • What would `GeneralDelegateForEmailAndSMS` actually do? It should be referenced by (and therefore owned) or interact with something. If you are using the delegate to again call class methods only, you might need to reconsider your code design. Delegates are always (best practice) assigned only and not retained by the picker, so autoreleasing it is indeed no option. – marcus Dec 26 '11 at 11:19
  • GeneralDelegateForEmailAndSMS is a VC does nothing but implements the methods the protocol of MFMailComposeViewControllerDelegate and MFMessageComposeViewControllerDelegate (It makes no difference treating it as NSObject subclass). If I made MyMailer a singleton instead of a 'class methods only' class, the MyMailer can implements those protocol method. Originally, there're many ViewControllers that send email/sms with different data, online tutorials just let each VC implement the MFxxxDelegate protoc and set delegate to self, which causes many boilerplate code. I wish STATIC Mailer may help. – user746403 Dec 26 '11 at 14:14

0 Answers0