objective-c - 在太多对象中初始化某些值会使我的应用程序崩溃

标签 objective-c ios singleton

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

@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 放入了多少个对象,这些对象的值存储在 ingredientQuants 中,但是在多次声明之后,它厌倦了让那些确实声明了 ingredientQuants 的对象,即使变量声明正确在我把它们藏在那里之前,然后它决定在 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]);

}

然后我初始化这个对象,获取它的成员数组并将它们放在 View Controller 中:

        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 时,它显示类别列表。我看到了这一点,但是当 View 被插入堆栈时出现时,它开始吐出所有器官并死亡。

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

最佳答案

将 NSStrings 和 NSArrays 的属性设置为复制而不是保留,并且在你的初始化方法中不要在你的成分列表和 espressoIList 上使用 [[NSArray alloc] initWithObjects:] - 你不要对其他数组这样做(这是正确的) .否则你会发生内存泄漏。像处理大多数其他数组一样使用 NSArray arrayWithObjects。

编辑:

也改变这个

menuItemTwo.ingredientList = espressoIList;

menuItemTwo.ingredientList = [NSArray arrayWithArray: espressoIList ];

编辑:

既然你说它没有帮助你进行这些调整,也许发布代码 你用//more lik this 表示

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

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

如果您将其排除在外,它就可以正常工作吗?

编辑:

在我看来,nyQuants 是在您尝试将其添加到 mocha 数组时发布的。 似乎 autoreleasepool 被耗尽并且对象消失了 - 尽管我认为这只会在运行循环之后发生。 尝试使用 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 一样分配,并在你的方法返回之前释放它们。

关于objective-c - 在太多对象中初始化某些值会使我的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10427163/

相关文章:

ios - 如何避免 MKMapView 刷新?

IOS-内存管理指针

ios - UITextView 在 iOS 7 上绘制不正确

iphone - 你如何创建自定义相机 View ,而不是 UIImagePickerViewController?

ios - 安全地访问 iOS 中的文件

c++ - 单例模式析构函数 C++

objective-c - 在 Swift 中创建等效的 Objective-C Getter 和 Setter

ios - AVPlayerItemDidPlayToEndTimeNotification 在项目未完成播放时发布

iphone - 我的单例走在正确的轨道上吗?

c# - 在高并发 WCF Web 服务中使用实例和单例