【问题标题】:Crash when loadingNibNamed in for loop在 for 循环中加载 NibNamed 时崩溃
【发布时间】:2017-02-03 15:19:12
【问题描述】:

这是下一个问题的延续

https://stackoverflow.com/questions/39673003/crash-on-multiple-calls-to-loadnibnamed

基本上我在打电话

[[NSBundle mainBundle] loadNibNamed:@"YFCalendarDayCell" owner:self options:nil] objectAtIndex:0];

NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
                                                     owner:nil
                                                   options:nil];

在 for 循环中。这最终会崩溃,BadExcess errorNSArray 返回的元素为零。

有正确的方法吗?

添加@Autoreleasepool

int i, j, k=0;
for( i=0; i<5; i++ )
{
    for( j=0; j<7; j++ )
    {
        @autoreleasepool {
        YFCalendarDayCell *tableView = [Utility loadNibNamed:@"YFCalendarDayCell" ofClass:[YFCalendarDayCell class]];

        tableView.cellTableView.dataSource = tableView;
        tableView.cellTableView.delegate = tableView;
        tableView.controller = mainViewController;

        //[calendarView addSubview:tableView];

        NSLog(@"tables added to calendar view i: %d j: %d",i,j);

        [calendarView.CellDict setObject:tableView forKey:[[NSNumber numberWithInt:k++] stringValue]];
        }
    }
}

【问题讨论】:

  • 您在第一行或第二行代码中遇到错误吗?
  • 尝试将循环体包裹在@autoreleasepool { } 中。也许您的内存不足。
  • @user3182143,第一个为 BadExcess,第二个为 0 个对象。这是我尝试加载 xib 的两种不同方式。
  • 您是否尝试过在 for 循环中使用自动释放池?
  • 正在尝试。给我一分钟

标签: ios objective-c loadnibnamed


【解决方案1】:

试试下面的答案

for (i=0; i<5; i++) {
      @autoreleasepool
      {
          for (j=0; j<7; j++)
          {

            YFCalendarDayCell *tableView = [Utility loadNibNamed:@"YFCalendarDayCell" ofClass:[YFCalendarDayCell class]];

            tableView.cellTableView.dataSource = tableView;
            tableView.cellTableView.delegate = tableView;
            tableView.controller = mainViewController;
            NSLog(@"tables added to calendar view i: %d j: %d",i,j);
            [calendarView.CellDict setObject:tableView forKey:[[NSNumber numberWithInt:k++] stringValue]];
          }
      }
}

【讨论】:

  • 试过了。收到此错误malloc: *** mach_vm_map(size=1048576) failed (error code=3) *** error: can't allocate region securely。多次通话后。
  • 谢谢。我会接受你的回答,因为它确实在一定程度上解决了这个问题。再次感谢。
  • 这个块属于NSOperationQueue的子类。所以我实际上不能在里面使用@autoreleasepool。还有其他方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多