我正在使用模拟数据填充单个对象,以在函数中吐出,该函数返回所有这些数据进入的数组。
@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
对象并不关心我使用带有存储在ingredientQuants中的值的menuItems放入了多少个对象,但是经过多次声明后,即使对于声明为的权限的变量,也会让那些已经声明了为IngredQuants的对象感到厌烦在我把它们藏在那里然后它决定在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]);
}
然后我初始化这个对象,抓住它的成员数组并将它们放在一个viewcontroller中:
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]:消息发送到解除分配的实例0x6e06550
答案 0 :(得分:1)
设置NSStrings和NSArrays的属性来复制而不是保留在你的init方法中不要在你的成分列表和espressoIList上使用[[NSArray alloc] initWithObjects:] - 你不要用其他数组做(这是正确的) 。否则会导致内存泄漏。像使用大多数其他数组一样使用NSArray arrayWithObjects。
编辑:
同时更改此
menuItemTwo.ingredientList = espressoIList;
要
menuItemTwo.ingredientList = [NSArray arrayWithArray: espressoIList ];
编辑:
由于您说它无法帮助您进行这些调整,因此可能会发布代码 你表示//更多喜欢这个
我真的不明白你为什么会遇到这个问题。造成撞车的线路是哪条? 这一个?
mocha.ingredientQuants = [NSArray arrayWithObjects:nyQuants, nil];
如果你把它留下来它只是有效吗?
编辑:
在我看来,当你试图将它添加到mocha数组时,nyQuants被释放了。 似乎autoreleasepool被耗尽并且对象消失了 - 尽管我认为这只会在运行循环之后发生。 尝试使用alloc init对你所依赖的nyQuants和其他数组进行硝化,并在完成所有menuItem的声明后手动释放它们。 我认为应该这样做!
所以,改变那些
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一样分配,并在方法返回之前释放它们。
答案 1 :(得分:0)
如果您收到NSArray - 发送到解除分配的实例的消息,意味着您正在使用具有负保留计数的变量,则以某种方式删除/释放它。
在头文件中,您使用(nonatomic, retain)
声明了数组,这意味着它们的保留计数为1.在m文件中,您应该在- (void)dealloc{}
方法中释放数组。您使用arrayWithObjects方法初始化数组,并且基于apple memory management描述,使用此方法您不拥有这些对象,因此您无法释放/删除/删除它们。