如何解决MFMailComposeViewControllerDelegate的子类的潜在泄漏

时间:2011-12-26 05:38:23

标签: ios memory-leaks delegates mfmailcomposer

方案, 为了DRY,我创建了一个实现协议MFMailComposeViewControllerDelegate方法的视图控制器(例如GeneralDelegateForEmailAndSMS)。

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

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

然后在负责邮件的类的类方法(例如composeDiscountInfo)中(例如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
}

我的问题是如何解决潜在的内存泄漏(上面的代码注释中提到)?我无法将委托设置为自动释放,因为它看起来像垃圾收集并崩溃应用程序。此外,MyMailer应该只包含类方法,没有生命周期方法来处理对象。解决方案是什么?

我对配置文件说明知之甚少,有关如何使用它来检测泄漏的任何提示?

更新

MyMailer应该是'仅类方法'类,在许多视图控制器中需要编写/发送应用内电子邮件/短信,我可以编写类似“[MyMailer composeMailWith:data]”的代码,其中列出了实现代码下面(即composeDiscountInfo:第二个代码片段中的数据)。

在这些类方法中,我希望我可以将MFMailCompseViewController的委托属性(mailComposeDelegate)设置为实现MFMailComposeViewControllerDelegate的独立类。

也许我会让Mailer成为一个单身人士,在许多VC中,调用看起来像'[MyMailer instance] composeThatMailWith:data'。我明天会试试!

0 个答案:

没有答案