AliSoftware / OHAttributedLabel

UILabel that supports NSAttributedString
https://github.com/AliSoftware/OHAttributedLabel/wiki
1.51k stars 344 forks source link

[OHAttributedLabel drawTextInRect:] crash #145

Closed nguyentanhoangvu closed 10 years ago

nguyentanhoangvu commented 11 years ago

https://github.com/AliSoftware/OHAttributedLabel/issues/82 This is another problem of drawTextInRect method. This issue is frequency crash, so it is difficult to reproduce crashing, so I cannot get more detail to investigate. Please help me! Crash log is as below:

Thread 0 Crashed: 1 libobjc.A.dylib _objc_msgSend + 16 2 DemoApplication -OHAttributedLabel drawTextInRect: 3 UIKit -[UILabel drawRect:] + 59 4 UIKit -[UIView(CALayerDelegate) drawLayer:inContext:] + 265 5 QuartzCore -[CALayer drawInContext:] + 93 6 QuartzCore ZL16backing_callbackP9CGContextPv + 39 7 QuartzCore _CABackingStoreUpdate + 1233 8 QuartzCore -[CALayer _display] + 731 9 QuartzCore -[CALayer display] + 141 10 QuartzCore _CALayerDisplayIfNeeded + 185 11 QuartzCore ZN2CA7Context18commit_transactionEPNS_11TransactionE + 221 12 QuartzCore ZN2CA11Transaction6commitEv + 191 13 QuartzCore ZN2CA11Transaction17observer_callbackEP19CFRunLoopObservermPv + 57 14 CoreFoundation _CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 17 15 CoreFoundation _CFRunLoopDoObservers + 413 16 CoreFoundation ___CFRunLoopRun + 855 17 CoreFoundation _CFRunLoopRunSpecific + 231 18 CoreFoundation _CFRunLoopRunInMode + 59 19 GraphicsServices _GSEventRunModal + 115 20 GraphicsServices _GSEventRun + 63 21 UIKit -[UIApplication _run] + 405 22 UIKit _UIApplicationMain + 671 23 Cablevision main (main.m:13)

AliSoftware commented 11 years ago

It seems to be a memory managment problem in your usage of OHAttrbutedLabel. Make sure that you have a strong reference (and not a weak reference) to your OHAttributedLabel instance, and more importantly (as it seems to be revealed by your crashlog) that you did not keep any reference to an OHAttributedLabel instance even after it has been deallocated.

In your case, it seems that you have an observer on some property that triggers an action to your OHAttributedLabel, but that OHAttributedLabel does not exists anymore (has been deallocated in the meantime), but you forgot to remove your observer. Removing the observer when the label is deallocated will then solve the problem (which would not be related to OHAttributedLabel by itself but is a memory mgmt issue in your code)

Migrating your project to ARC will probably be another solution to solve your problem, because the instance will be correctly deallocated and weak references to it will be set to nil (thanks to Zeroing-Weak References)

nguyentanhoangvu commented 11 years ago

Thanks for your comment. My project is using ARC. I use 7 OHAttributedLabel instances. Once of them, I used retain reference (I have just changed retain to strong reference) and I sure that I use strong reference for other. I don't have any observer on any property to triggers an action to OHAttributedLabel. Maybe I am having about memory management problem. I will investigate. Thanks again.