В моем классе Orders у меня есть 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;
}
Этот класс OrderSummary содержит 3 NSMutableDictionaries и 1 NSMutableArray, все хранящие ссылку на объект 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 секунд для обновления представления.