ClassyKit / Classy

Expressive, flexible, and powerful stylesheets for UIView and friends.
http://classykit.github.io/Classy/
MIT License
740 stars 76 forks source link

EXC_BAD_ACCESS #82

Closed latteier closed 7 years ago

latteier commented 10 years ago

I'm sometimes getting a crash with Classy

0   ???                                 0x1295079c 0x0 + 311756700,
1   reacht                              0x000aefb0 main + 0,
2   reacht                              0x002678d4 -[CASStyleSelector matchesAncestorsOfItem:traverse:] + 436,
3   reacht                              0x00266f0f -[CASStyleSelector shouldSelectItem:] + 271,
4   reacht                              0x00268d8a -[CASStyler styleItem:] + 1754,
5   reacht                              0x0027bd1a -[UIView(CASAdditions) cas_updateStyling] + 154,
6   reacht                              0x0027b5a0 -[UIView(CASAdditions) cas_didMoveToWindow] + 48,
7   UIKit                               0x022b51c9 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1703,
8   UIKit                               0x022b4e4f -[UIView(Internal) _didMoveFromWindow:toWindow:] + 813,
9   UIKit                               0x022b4e4f -[UIView(Internal) _didMoveFromWindow:toWindow:] + 813,
10  UIKit                               0x022c7fd1 -[UIScrollView _didMoveFromWindow:toWindow:] + 65,
11  UIKit                               0x022b4e4f -[UIView(Internal) _didMoveFromWindow:toWindow:] + 813,
12  UIKit                               0x022b4e4f -[UIView(Internal) _didMoveFromWindow:toWindow:] + 813,
13  UIKit                               0x022b4e4f -[UIView(Internal) _didMoveFromWindow:toWindow:] + 813,
14  UIKit                               0x022ac137 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 154,
15  UIKit                               0x022ac095 -[UIView(Hierarchy) _postMovedFromSuperview:] + 458,
16  UIKit                               0x022b7b9e -[UIView(Internal) _addSubview:positioned:relativeTo:] + 2067,
17  UIKit                               0x022aa48e -[UIView(Hierarchy) addSubview:] + 56,
18  UIKit                               0x02606b46 -[UINavigationTransitionView transition:fromView:toView:] + 501,
19  UIKit                               0x02606948 -[UINavigationTransitionView transition:toView:] + 56,
20  UIKit                               0x023bdc1a -[UINavigationController _startTransition:fromViewController:toViewController:] + 3124,
21  UIKit                               0x023bdfdb -[UINavigationController _startDeferredTransitionIfNeeded:] + 669,
22  UIKit                               0x023bec52 -[UINavigationController __viewWillLayoutSubviews] + 57,
23  UIKit                               0x02530ebc -[UILayoutContainerView layoutSubviews] + 213,
24  UIKit                               0x022ba9c0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 608,
25  libobjc.A.dylib                     0x036ee771 -[NSObject performSelector:withObject:] + 70,
26  QuartzCore                          0x01fcf27f -[CALayer layoutSublayers] + 152,
27  QuartzCore                          0x01fc3105 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 397,
28  QuartzCore                          0x01fc2f60 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26,
29  QuartzCore                          0x01f21676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284,
30  QuartzCore                          0x01f22a3c _ZN2CA11Transaction6commitEv + 392,
31  QuartzCore                          0x01fe8789 +[CATransaction flush] + 52,
32  UIKit                               0x0225924b _afterCACommitHandler + 126,
33  CoreFoundation                      0x0436cfbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30,
34  CoreFoundation                      0x0436cf00 __CFRunLoopDoObservers + 400,
35  CoreFoundation                      0x0436293a __CFRunLoopRun + 1226,
36  CoreFoundation                      0x043621ab CFRunLoopRunSpecific + 443,
37  CoreFoundation                      0x04361fdb CFRunLoopRunInMode + 123,
38  GraphicsServices                    0x0607c24f GSEventRunModal + 192,
39  GraphicsServices                    0x0607c08c GSEventRun + 104,
40  UIKit                               0x0222fe16 UIApplicationMain + 1526,
41  reacht                              0x000af0af main + 255,
42  libdyld.dylib                       0x04b1eac9 start + 1

Not sure what other information you need.

line 122 of CAStyleSelector.m is higlighted

} else if ([self matchesItem:currentItem.cas_alternativeParent]) {
latteier commented 10 years ago

I should say that I'm using the master branch of Classy from git.

latteier commented 10 years ago

Seems to happen when I use the "iPhone 4s", "iPhone 5", and "iPad 2" simulators, but not "iPhone 5s", "iPhone 6 Plus", or "iPhone 6".

AndersHqst commented 10 years ago

I get a similar EXC_ACCESS error on line 118

while (currentItem.cas_parent != nil || currentItem.cas_alternativeParent != nil) {

It happens when I tap the "Cancel" button on a UIActionSheet. Changing the line to:

while (currentItem.cas_parent != nil) {

makes the error go away. Only on iOS8

itvexesme commented 10 years ago

The same issue affect Classy 0.2.4 on iOS7/8 version.

Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x00000001956580b4 objc_retain + 20
1  App                     0x0000000100215f70 -[CASStyleSelector matchesAncestorsOfItem:traverse:] (CASStyleSelector.m:122)
2  App                     0x00000001002158f4 -[CASStyleSelector shouldSelectItem:] (CASStyleSelector.m:79)
3  App                     0x0000000100216778 -[CASStyler styleItem:] (CASStyler.m:63)
4  App                     0x000000010021f820 -[UIView(CASAdditions) cas_updateStyling] (UIView+CASAdditions.m:69)
5  App                     0x000000010021f528 -[UIView(CASAdditions) cas_didMoveToWindow] (UIView+CASAdditions.m:25)
6  UIKit                          0x00000001897895cc -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1464
7  UIKit                          0x00000001897892e4 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
8  UIKit                          0x0000000189807810 -[UIControl _didMoveFromWindow:toWindow:] + 60
9  UIKit                          0x00000001897892e4 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
10 UIKit                          0x00000001897aa954 -[UIScrollView _didMoveFromWindow:toWindow:] + 68
11 UIKit                          0x00000001897892e4 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
12 UIKit                          0x00000001897892e4 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
13 UIKit                          0x00000001897892e4 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
14 UIKit                          0x00000001897892e4 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720
15 UIKit                          0x00000001897889b4 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 144
16 Foundation                     0x0000000185e36368 -[NSISEngine withBehaviors:performModifications:] + 176
17 UIKit                          0x0000000189788874 -[UIView(Hierarchy) _postMovedFromSuperview:] + 452
18 UIKit                          0x0000000189794330 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1724
19 UIKit                          0x000000018987fe3c -[UITransitionView transition:fromView:toView:removeFromView:] + 1228
20 UIKit                          0x000000018987f434 -[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 984
21 UIKit                          0x000000018987eb74 __101-[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:]_block_invoke_2 + 1356
22 UIKit                          0x000000018980f388 _applyBlockToCFArrayCopiedToStack + 356
23 UIKit                          0x0000000189780e4c _afterCACommitHandler + 532
24 CoreFoundation                 0x0000000184fc2388 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
25 CoreFoundation                 0x0000000184fbf314 __CFRunLoopDoObservers + 360
26 CoreFoundation                 0x0000000184fbf6f4 __CFRunLoopRun + 836
27 CoreFoundation                 0x0000000184eed664 CFRunLoopRunSpecific + 396
28 GraphicsServices               0x000000018e0275a4 GSEventRunModal + 168
29 UIKit                          0x00000001897f2984 UIApplicationMain + 1488
30 App                     0x0000000100072f28 main (main.m:17)
31 libdyld.dylib                  0x0000000195caea08 start + 4
latteier commented 10 years ago

It appears to me that the crash is caused by cas_alternativeParent

(lldb) po [currentItem cas_alternativeParent]
0x00007ff4aae9ef60

That said, I'm not sure how it got into this state.

latteier commented 10 years ago

OK, I think I know what happened for me.

The currentItem is a view that comes from another view controller. That view controller sets the cas_alternativeParent on the view to itself. Then I add the view to my view hierarchy. The view controller goes away because the cas_alternativeParent is a weak property. Later when trying to style the view there's a bad reference to its original view controller.

cloudkite commented 10 years ago

Has anyone found a fix? If so please submit a PR and I will try merge into master. thanks!

latteier commented 10 years ago

It's unclear with a good fix is.

The issue is that cas_alternativeParent isn't safe. If the view controller that it refers to goes away then it becomes a bad reference.

What I did to deal with it was to manually set my view's cas_alternativeParent to nil when I know that its original view controller is going away.

SpaceFlightOrange commented 10 years ago

Im seeing this only when a user dismisses a UIAlertView. The implementation of this has changed in iOS8. the offending controller seems to be _UIAlertShimPresentingViewController.

If I switch to the new UIAlertController the issue goes away. The issue does not occur on iOS 7

itvexesme commented 9 years ago

Current crash logs reports that issue is only for iOS 7 devices.

imoreapps commented 9 years ago

Hi guys, the issue exists on iOS 7 & 8. I just fixed it, hope it will be merged into master. The root of the problems begin with OBJC_ASSOCIATION_ASSIGN, it equals the "unsafe_unretained", we should use the "weak". Below link is my patch codes.

https://www.dropbox.com/s/dkmzp8x4ymxdjz4/Classy-ios8-patch.zip?dl=0

mikeger commented 9 years ago

@iMoreApps can you create a pull request for your changes? This would be very helpful

EDIT: did it for you

imoreapps commented 9 years ago

Thanks mikeger.

dnedrow commented 7 years ago

@latteier , a solution was merged into Classy. Can you close this issue?

latteier commented 7 years ago

Glad to hear that it's fixed.