Hello. I'm receiving this crash report (iOS 10 only) for my app:
Crashed: com.apple.main-thread
0 libsystem_kernel.dylib 0x181740d74 __abort_with_payload + 8
1 libsystem_kernel.dylib 0x18173d480 abort_with_payload_wrapper_internal + 100
2 libsystem_kernel.dylib 0x18173d41c abort_with_payload_wrapper_internal + 34
3 libobjc.A.dylib 0x1811abed8 _objc_fatalv(unsigned long long, unsigned long long, char const*, char*) + 112
4 libobjc.A.dylib 0x1811abe30 __objc_error + 42
5 libobjc.A.dylib 0x1811becd4 weak_entry_insert(weak_table_t*, weak_entry_t*) + 318
6 libobjc.A.dylib 0x1811c4664 objc_initWeak + 324
7 UIKit 0x188642e90 -[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] + 2068
8 UIKit 0x1885c2830 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1352
9 UIKit 0x1885c25b8 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
10 UIKit 0x1885c1a64 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 156
11 UIKit 0x1885c18bc -[UIView(Hierarchy) _postMovedFromSuperview:] + 792
12 UIKit 0x1888bee58 __UIViewWasRemovedFromSuperview + 172
13 UIKit 0x1885c0954 -[UIView(Hierarchy) removeFromSuperview] + 512
14 MyApp removes subview from superview
In line 14 i removed custom view from superview, that view has subview which was created from xib file like this:
- (UIView *)createCustomSubview
{
UIViewController* vc = [UIViewController new];
MyCustomView* customView = [customViewNib instantiateWithOwner:vc options:nil][0];
return customView;
}
When debug this issue, debugger says:
Cannot form weak reference to instance (address) of class UIViewController. It is possible that this object was over-released, or is in the process of deallocation.
And it seems that instance of class UIViewController is that vc variable from - (UIView *)createCustomSubview. So it seems like view controller which is file's owner of uiview is deallocated while that view is removing from superview.
I'm, for now, solve this problem by wrap - (UIView *)createCustomSubview content to @autoreleasepool block.
So my question is: is it correct to instantiate nib that way (with temporary view controller as file's owner)?. Do i need to retain file's owner of nib file? What happens when file's owner of xib is deallocated? How can i instantiate multiple different instances of that custom view to avoid this crash?