1

我正在用模拟数据填充一个单例对象,以吐出一个函数,该函数返回所有这些数据进入的数组。

@interface menuItem : NSObject {
//Item's Name
NSString *itemName;
//String of Prices - "$4.50" or "$1.50 / $2.50 / $3.50"
NSArray *priceList;
//List of Ingredients 
NSArray *ingredientList;
//List of adjusted Quantities
NSArray *ingredientQuants; 
//Number of Sizes for corresponding prices
int numOfSizes;
}
- (NSString *)priceText;
@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSArray *priceList;
@property (nonatomic, copy) NSArray *ingredientList;
@property (nonatomic, copy) NSArray *ingredientQuants;
@property (nonatomic, assign) int numOfSizes;
@end

该对象不关心我将多少个对象与 menuItems 一起放入其中,而这些对象的值存储在成分Quants 中,但是在多次声明之后,它厌倦了让那些确实声明了成分Quants 的对象,即使是在我存储之前声明的变量他们在那里,然后它决定在 main.c 上执行 EXEC_BAD_ACCESS

-(NSArray *)returnAllItems {
  return items;
}

- (void) loadMenu { 
   if(categories && items){ 
     return;
}
categories = [[NSArray alloc] initWithObjects:@"Coffee", @"Coffee Alternatives", @"Smoothies", @"Baked Goods", @"Sandwiches",@"Salads", nil];

NSArray *nyQuants = [[NSArray alloc] initWithObjects:@"No", @"Yes", nil];
//NSArray *ynQuants = [NSArray arrayWithObjects:@"Yes", @"No", nil];
NSArray *numQuants = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"0", nil];
NSArray *espressoIList = [[NSArray alloc] initWithObjects:@"Decaf", @"Iced", @"Soymilk", @"Shots",nil];

menuItem *menuitemOne = [[menuItem alloc] init];
menuitemOne.itemName = @"Regular Coffee";
menuitemOne.priceList = [NSArray arrayWithObjects:@"$1.50",@"$1.95",@"$2.15", nil];
menuitemOne.numOfSizes = 3;
// menuitemOne.ingredientList = [NSArray arrayWithObjects:@"Decaf", @"Iced", nil];
//menuitemOne.ingredientQuants = [NSArray arrayWithObjects:nyQuants, nyQuants, nil];

menuItem *menuItemTwo = [[menuItem alloc] init];
menuItemTwo.itemName = @"Latte";
menuItemTwo.priceList = [NSArray arrayWithObjects:@"$2.55", @"$3.45", @"$3.75", nil];
menuItemTwo.numOfSizes = 3;
menuItemTwo.ingredientList = espressoIList;
menuItemTwo.ingredientQuants = [NSArray arrayWithObjects:nyQuants, nyQuants, nyQuants, numQuants, nil];

menuItem *mocha = [[menuItem alloc]init];
mocha.itemName = @"Mocha";
mocha.priceList = [NSArray arrayWithObjects:@"$3.15",@"$3.95",@"$4.75", nil];
mocha.numOfSizes = 3;
mocha.ingredientList = espressoIList;    
//THIS LINE BREAKS THE ENTIRE PROGRAM
NSArray *aaaa = [NSArray arrayWithObjects:@"ASD", @"DFG", nil];
mocha.ingredientQuants = [NSArray arrayWithObjects:aaaa, nil];

//更多的事情像上面一样发生

items = [NSArray arrayWithObjects:coffeeArray, espressoArray,smoothieArray, bakedArray, sandwichArray,saladArray, nil];

[nyQuants release];
[numQuants release];
[espressoIList release];

NSLog(@"Categories: %d", [categories retainCount]);
NSLog(@"items: %d", [items retainCount]);
NSLog(@"nyQuants: %d", [nyQuants retainCount]);
NSLog(@"numQuants: %d", [numQuants retainCount]);
NSLog(@"espresslist: %d", [espressoIList retainCount]);

}

然后我初始化这个对象,获取它的成员数组并将它们放在一个视图控制器中:

        CBMenu *WholeMenu = [CBMenu sharedInstance];
    NSLog(@"MENU");
    NSArray *cats = [WholeMenu returnCategories];
    NSLog(@"Cats");
    NSArray *menu = [WholeMenu returnAllItems];
    NSLog(@"Menu");

    //What are these I don't even
    [cats retain];
    [menu retain];

    FoodMenuTwoViewController *mmvc = [[FoodMenuTwoViewController alloc]initWithData:cats :NO: YES];
    [mmvc setDataSource:cats];
    [mmvc setMenu:menu];
    [mmvc setTitle:@"Menu"];
    [self.navigationController pushViewController:mmvc animated:YES];

    //FOR SOME REASON IT DOESN'T LIKE ME RELEASING CATS AND MENU OR REMOVING THEIR
    //RETAIN STATEMENTS ABOVE PLEASE DON'T HURT ME.

    return;

当 VC 中的第一个 bool 为 YES 时,它会显示类别列表。我看到了这一点,但是当视图被推入堆栈时出现,它会继续呕吐其所有器官并死亡。

NSZombieEnabled 告诉我 CharlieBrown[4285:fb03] * -[__NSArrayI retain]: message sent to deallocated instance 0x6e06550

4

2 回答 2

1

将 NSStrings 和 NSArrays 的属性设置为复制而不是保留,并且在您的 init 方法中不要在您的成分列表和 espressoIList 上使用 [[NSArray alloc] initWithObjects:] - 您不要对其他数组执行此操作(这是正确的)。否则你会得到内存泄漏。像使用其他大多数数组一样使用 NSArray arrayWithObjects。

编辑:

也改变这个

menuItemTwo.ingredientList = espressoIList;

menuItemTwo.ingredientList = [NSArray arrayWithArray: espressoIList ];

编辑:

既然你说它没有帮助你进行这些调整,也许可以发布你用//更多这样的代码

我真的不明白你为什么会遇到这个问题。哪条线路导致崩溃?这个?

mocha.ingredientQuants = [NSArray arrayWithObjects:nyQuants, nil];

如果你把它排除在外,它就可以工作吗?

编辑:

在我看来,当您尝试将 nyQuants 添加到 mocha 数组时,它已被释放。似乎自动释放池被耗尽并且对象消失了——尽管我认为这只会在运行循环之后发生。尝试使用 alloc init 初始化您的 nyQuants 和其他数组,并在完成所有 menuItems 的声明后手动释放它们。我认为应该这样做!

所以,改变那些

NSArray *nyQuants = [NSArray arrayWithObjects:@"No", @"Yes", nil];
NSArray *ynQuants = [NSArray arrayWithObjects:@"Yes", @"No", nil];
NSArray *numQuants = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6",     @"7", @"8", @"0", nil];
NSArray *espressoIList = [[NSArray alloc] initWithObjects:@"Decaf", @"Iced", @"Soymilk", @"Shots",nil];

像 espressoIList 一样要分配的所有内容都在此示例中,并在您的方法返回之前释放它们。

于 2012-05-03T08:06:57.290 回答
0

If you are getting a NSArray - message sent to deallocated instance, means that you are using a variable with a negative retain count, you deleted/released it somehow.

In the header file you declared your arrays with (nonatomic, retain), it means that their retain count is 1. In the m file you should deallocate the arrays in the - (void)dealloc{} method. You initialized your arrays with arrayWithObjects method, and based on the apple memory management description, with this method you don't own these object, so you cannot release/delete/remove them.

于 2012-05-03T08:10:07.427 回答