内存泄漏问题

时间:2011-05-02 14:29:40

标签: iphone memory-leaks

在我的课程订单中,我有2个函数'generateOrderSummary'& 'createOrderSummaryItem':

- (OrderSummary *)generateOrderSummary {


//Cleaning the references but OrderSummaryItem leaks
[self.currentOrderSummary removeAllOrderedItems];
self.currentOrderSummary = nil;

for(MenuItem *menuItem in self.selectedItems) {

    OrderSummaryItem *orderSummaryItem = [self createOrderSummaryItem:menuItem];
    //Retain count = 1

    [orderSummary addOrderedItem:orderSummaryItem withServiceStationCode:menuItem.serviceStationCode withCategory:menuItem.category withCourseOrder:menuItem.courseOrder];
   //Retain count = 5;
}

self.currentOrderSummary = orderSummary;
[orderSummary release];
orderSummary = nil;

return self.currentOrderSummary;    
}

- (OrderSummaryItem *) createOrderSummaryItem:(MenuItem *)menuItem {

OrderSummaryItem *summaryItem = [[[OrderSummaryItem alloc]init] autorelease];

//Set summaryItem properties    

return summaryItem;
}

这个带有3个NSMutableDictionaries和1个NSMutableArray的OrderSummary类都保持对OrderSummaryItem对象的引用。

-(void)addOrderedItem:(OrderSummaryItem *)orderedItem withServiceStationCode:(NSString *)serviceStationCode withCategory:(NSString *)category withCourseOrder:(NSString *)courseOrder {

if (self.serviceStationDict == nil) {
    self.serviceStationDict = [NSMutableDictionary dictionaryWithCapacity:1];
}
if (self.categoryDict == nil) {
    self.categoryDict = [NSMutableDictionary dictionaryWithCapacity:1];
}
if (self.courseOrderDict == nil) {
    self.courseOrderDict = [NSMutableDictionary dictionaryWithCapacity:1];
}
if(self.orderedItems == nil) {
    self.orderedItems = [NSMutableArray arrayWithCapacity:1];
}

if(serviceStationCode != nil) {
    NSMutableArray *orderedItemsForServiceStation = [self.serviceStationDict objectForKey:serviceStationCode];
    if (orderedItemsForServiceStation != nil) {
        [orderedItemsForServiceStation addObject:orderedItem];
    }
    else {
        orderedItemsForServiceStation = [NSMutableArray arrayWithCapacity:1];
        [orderedItemsForServiceStation addObject:orderedItem];
        [self.serviceStationDict setObject:orderedItemsForServiceStation forKey:serviceStationCode];
       //Retain count = 2
    }
}

if(category != nil) {
    NSMutableArray *orderedItemsForCategory = [self.categoryDict objectForKey:category];
    if (orderedItemsForCategory != nil) {
        [orderedItemsForCategory addObject:orderedItem];
    }
    else {
        orderedItemsForCategory = [NSMutableArray arrayWithCapacity:1];
        [orderedItemsForCategory addObject:orderedItem];
        [self.categoryDict setObject:orderedItemsForCategory forKey:category];
        //Retain count = 3
    }
}

if(courseOrder != nil) {
    NSMutableArray *orderedItemsForCourseOrder = [self.courseOrderDict objectForKey:courseOrder];
    if (orderedItemsForCourseOrder != nil) {
        [orderedItemsForCourseOrder addObject:orderedItem];
    }
    else {
        orderedItemsForCourseOrder = [NSMutableArray arrayWithCapacity:1];
        [orderedItemsForCourseOrder addObject:orderedItem];
        [self.courseOrderDict setObject:orderedItemsForCourseOrder forKey:courseOrder];   
       //Retain count = 4
    }
}
[self.orderedItems addObject:orderedItem];  
      //Retain count = 5
}

-(void)removeAllOrderedItems{

[self.serviceStationDict removeAllObjects];
self.serviceStationDict = nil;
[self.categoryDict removeAllObjects];
self.categoryDict = nil;
[self.courseOrderDict removeAllObjects];
self.courseOrderDict = nil;
[self.orderedItems removeAllObjects];
self.orderedItems = nil;
}

我无法找到为什么我的OrderSummaryItem在清理它时会泄漏。我的'generateOrderSummary'函数每5秒调用一次以刷新视图。

有人可以帮忙吗?

提前完成。

2 个答案:

答案 0 :(得分:1)

如果你声明你的数组s getter and setter than after alloc it so u have to Release **twice** because it的保留计数变为2,那就试试......

头脑优先的Iphone开发在本书中解释得很好......

答案 1 :(得分:0)

尝试在此处添加自动释放池:

for(MenuItem *menuItem in self.selectedItems) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
OrderSummaryItem *orderSummaryItem = [self createOrderSummaryItem:menuItem];
//Retain count = 1

[orderSummary addOrderedItem:orderSummaryItem withServiceStationCode:menuItem.serviceStationCode withCategory:menuItem.category withCourseOrder:menuItem.courseOrder];
//Retain count = 5;
[pool release];}

也许没有泄漏,只是主池不经常流失