iphone - 使用 NSMutableArrays 创建 NSDictionary 的内存管理

标签 iphone objective-c cocoa-touch ios5 memory-management

在使用可变数组创建字典时,我在理解内存管理方面遇到了问题。我正在使用 ios6 SDK 和部署目标 5.1。

在类“Group”的实现中,方法“namesAndEmails”构建了一个数组“emails”,其中包含带有电子邮件的 Person 对象的电子邮件地址。如果 Person 对象没有电子邮件,则将 Person 名称添加到另一个数组“namesWithNoEmail”中。数组在字典中返回。

#import "Group.h"
@implementation Group 

-(NSDictionary*) namesAndEmails {
    NSMutableArray  *emails = [[NSMutableArray alloc] initWithCapacity:0] ;
    NSMutableArray  *namesWithNoEmail = [[NSMutableArray alloc] initWithCapacity:0];
    NSString *email;
    NSString *name;
    for (Person *p in allPersons) {
        email = p.email;
        name = p.name;
        if ([email length]==0) {
            [namesWithNoEmail addObject:name];
        } else {
            [emails addObject:email];
        }
    }
    NSArray *keys = [NSArray arrayWithObjects:@"emails",@"names", nil];
    NSArray *objects = [NSArray arrayWithObjects:emails, namesWithNoEmail, nil];
    //[emails release];
    //[namesWithNoEmail release];

     return [NSDictionary dictionaryWithObjects:objects forKeys:keys];
}

在代码的其他地方,我希望向一组人发送电子邮件,因此我调用 emailGroup 方法,该方法通过调用组上的“namesAndEmails”来获取字典。

-(void) emailGroup:(Group*) g {

    NSDictionary *emailInfo = [g namesAndEmails];
    guestsWithNoEmail = [emailInfo objectForKey:@"names"];
    guestEmails = [emailInfo objectForKey:@"emails"];

   int nGuestsWithNoEmail = [guestsWithNoEmail count];

   if (nGuestsWithNoEmail > 0) {
      UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"No emails" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
      [alert show];
   }

    // some more code here


    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:subject];
    [picker setMessageBody:@"" isHTML:NO];
    [picker setToRecipients:guestEmails];
    [[self delegate ] presentModalViewController:picker animated:YES];
    [picker release];
}

据我了解,“namesAndEmails”中的 [NSDictionary DictionaryWithObjects:objects forKeys:keys] 返回一个自动释放的字典。但是,如果我释放“emails”和“namesWithNoEmail”数组,为什么我的代码会崩溃?我认为字典在添加数组后将拥有数组的所有权,因此在方法中释放数组是安全的。我想这是不正确的,但为什么呢?

有更干净的方法吗?谢谢您的建议!

最佳答案

我的第一个建议是使用“产品->分析”功能。如果您在某个地方泄漏或过度释放,它可能会给您准确的事件链。

其次,我看不到您的方法 nameAndEmailsemailGroup: 之间的链接。因为我看不到连接,所以无法告诉您问题是否是由自动释放引起的。

当主运行循环循环时,自动释放的对象被释放。所以你的 NSDictionary 很可能会被释放。您可以通过执行任何操作来测试这一点,从将内存位置设置为调试器中的“ watch ”,到每次运行循环循环时在控制台行中打印一些内容(我假设您在主运行循环中,所以纠正我如果这不是真的)。​​

您可以做的其他事情来跟踪问题是在仪器中使用“Zombies”或在您的配置中使用NSZombieEnable=YES

关于iphone - 使用 NSMutableArrays 创建 NSDictionary 的内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12981447/

相关文章:

IOS调用函数给出错误

ios - 具有居中 UIImageView 的 UIScrollView,如照片应用

iphone - IOS中keychainWrapper中存放NSArray或NSDictionary

iphone - 内置保存按钮文本

iphone - 从 App Delegate 中提取的全局变量

cocoa-touch - 如何在 UIImageView 中平移图像?

iphone - UISegmentControl配置

iphone - Cocos2d iPhone 中的 NSNotification

ios - 在 UIWebView 中禁用下载

iphone - 核心数据后台线程 NSManagedObjectContext 合并错误