在iphone中分配的对象的潜在泄漏

时间:2012-04-09 05:02:31

标签: iphone objective-c ios memory-management memory-leaks

当我分析我的项目后,代码给我泄漏警告。有没有办法解决我的内存泄漏问题?

警告:

Potential leak of an object allocated on line 38 and stored into 'addressBook'

贝娄是我的代码。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    m_tableDataArray = [[[NSMutableArray alloc] init]autorelease];
    NSMutableArray *listDate = [[[NSMutableArray alloc] init]autorelease];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSInteger addressesCount = [addresses count];
    for (int i = 0; i < addressesCount; i++) {
        ABRecordRef record = [addresses objectAtIndex:i];
        NSString *firstName = (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
        NSString *lastName = (NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
        if(firstName != nil && lastName != nil){
            NSString *contactFirstLast = [NSString stringWithFormat: @"%@ %@", firstName, lastName];
            [listDate addObject:contactFirstLast];
        }
        [firstName release];
        [lastName release];
    }
    m_tableDataArray = [[NSArray arrayWithArray:listDate] retain];
    [addresses release];
    addresses = nil;
    [m_mainTable reloadData];
}

先谢谢...

1 个答案:

答案 0 :(得分:6)

使用完{1}后,您需要使用以下内容发布它:

addressBook

这应该放在CFRelease(addressBook); 方法的末尾。

已更新:您的viewWillAppear:版本中有一些不必要的数组和步骤。我已经清理了一下并修复了潜在的内存泄漏。

注意:我实际上并没有这样做,请仔细检查它是否正常工作。

viewWillAppear: