内存管理问题

时间:2011-09-14 13:49:54

标签: iphone objective-c

我在函数[Method。]

中分配了对象

解析器内部方法。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict{

    Information *aInfo = [[Information alloc] init];

    if([elementName isEqualToString:@"data"]){

        aInfo.strStoreId = [attributeDict valueForKey:@"storeid"];
        [arrayList addObject:aInfo];
    } else if ([ActionType isEqualToString:@"action"]) {

        if([elementName isEqualToString:@"data"]) {
            aInfo.strStoreId = [attributeDict valueForKey:@"storeid"];
           [arrayList addObject:aInfo];
        }

    }
}

在这种情况下如何管理内存问题?

3 个答案:

答案 0 :(得分:6)

方法结尾处的

[aInfo release];[aInfo autorelease];

答案 1 :(得分:2)

您可以释放该对象,将其添加到数组中增加了保留计数。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict{

    Information *aInfo = [[Information alloc] init];

    if([elementName isEqualToString:@"data"]){

        aInfo.strStoreId = [attributeDict valueForKey:@"storeid"];
        [arrayList addObject:aInfo];
    } else if ([ActionType isEqualToString:@"action"]) {

        if([elementName isEqualToString:@"data"]) {
            aInfo.strStoreId = [attributeDict valueForKey:@"storeid"];
           [arrayList addObject:aInfo];
        }

    }

    [aInfo release], aInfo = nil;
}

答案 2 :(得分:1)

您还可以将aInfo变量定义为类变量,并使用以下方法释放它:

– parser:didEndElement:namespaceURI:qualifiedName: