1

This is in continuation to the following question

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

Basically i am calling

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

OR

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

in a for loop. This eventually crashes with BadExcess error OR NSArray that is returned has zero elements.

Is there a correct way to do this?

After Adding @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]];
        }
    }
}
Community
  • 1
  • 1
LeXeR
  • 214
  • 3
  • 20

1 Answers1

1

Try below answer

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]];
          }
      }
}
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • Tried. Got this error `malloc: *** mach_vm_map(size=1048576) failed (error code=3) *** error: can't allocate region securely`. After multiple calls. – LeXeR Sep 26 '16 at 10:46
  • http://stackoverflow.com/questions/32755508/xcode-7-ios-9-iphone-4s-iphone5-only-issue-malloc-mach-vm-mapsize – user3182143 Sep 26 '16 at 10:50
  • Also please check with http://stackoverflow.com/questions/8477236/malloc-error-cant-allocate-region-failed-with-error-code-12-any-idea-how-to – user3182143 Sep 26 '16 at 10:52
  • Thanks. I will accept your answer because it did resolve the issue to some extent. Thanks again. – LeXeR Sep 26 '16 at 10:56
  • this block is in a class which is a subclass of `NSOperationQueue`. So i cannot actually use `@autoreleasepool` inside that. Any other ways of doing this? – LeXeR Oct 05 '16 at 11:36
  • http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc – user3182143 Oct 05 '16 at 11:54
  • Also this one http://stackoverflow.com/questions/16198477/ios-autorelease-pool-blocks – user3182143 Oct 05 '16 at 11:59
  • actually, loading of the nib is causing the problem when done in NSOperation. Maybe the two are going out of sync. Could you please help me with this. – LeXeR Oct 05 '16 at 12:03
  • or maybe one is release before the latter is loaded. Is there a way to wait for nib loading inside the nsoperation – LeXeR Oct 05 '16 at 12:04
  • From above link and others sources say loading multiple nib in auto release pool is better.If you go through that you can get idea. – user3182143 Oct 05 '16 at 12:09
  • http://stackoverflow.com/questions/19569244/nsoperation-and-nsoperationqueue-working-thread-vs-main-thread – user3182143 Oct 05 '16 at 12:12