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!