Please help with this error. It has baffled me for a few days now. The error is happening only on a few selected devices, but I'm unable to simulate the behaviour on my devices.
I have an app that shows notifications to the user. The notification Navigation Controller has a label that uses the following class to set some attributes
class LabelPaddingNotifications: UILabel {
var insets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
func padding(_ top: CGFloat, _ bottom: CGFloat, _ left: CGFloat, _ right: CGFloat) {
self.frame = CGRect(x: 0, y: 0, width: self.frame.width + left + right, height: self.frame.height + top + bottom)
insets = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: insets))
}
override var intrinsicContentSize: CGSize {
get {
var contentSize = super.intrinsicContentSize
contentSize.height += insets.top + insets.bottom + 50
contentSize.width += insets.left + insets.right
return contentSize
}
}}
The app crashes on the following line sometimes. I'm unable to simulate the behavior on my devices or simulators though. The following code is in the viewDidLoad of the class
labelNotification.layer.cornerRadius = _smallButtonCornerRadius
And the outlet is defined as
@IBOutlet var labelNotification: LabelPadding!
Small radius is defined in a constants file as follows
let _smallButtonCornerRadius : CGFloat = 7
I also have an extension to CALayer as follows
extension CALayer {
func shake(duration: TimeInterval = TimeInterval(0.5)) {
let animationKey = "shake"
removeAnimation(forKey: animationKey)
let kAnimation = CAKeyframeAnimation(keyPath: "transform.translation.x")
kAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
kAnimation.duration = duration
var needOffset = frame.width * 0.15,
values = [CGFloat]()
let minOffset = needOffset * 0.1
repeat {
values.append(-needOffset)
values.append(needOffset)
needOffset *= 0.5
} while needOffset > minOffset
values.append(0)
kAnimation.values = values
add(kAnimation, forKey: animationKey)
}
func pulsate() {
let pulse = CASpringAnimation(keyPath: "transform.scale")
pulse.duration = 0.5
pulse.fromValue = 0.95
pulse.toValue = 1.0
pulse.autoreverses = false
pulse.repeatCount = 0
pulse.initialVelocity = 0.5
pulse.damping = 1.0
add(pulse, forKey: nil)
}
}
Crashlytics is showing me the following error
The stack trace indicates that heap corruption may have caused your app to crash. Memory corruption can occur pretty easily from freeing a dangling pointer, a thread race, or bad pointer arithmetic. The important thing to keep in mind is that the resulting crash may happen long after the initial corruption. As a result, the stack trace for this crash might not provide any clues to the location of the bug in your code. However, you can still fix memory issues with tools from Apple. For speedy resolution of memory corruption issues, we recommend regularly auditing your app with Xcode’s memory debugging facilities: Visual Memory Debugger, Zombies Instrument, Address Sanitizer, Thread Sanitizer and malloc diagnostics.
While Crashes in XCode organizer shows me an error on the line above giving me the following
Update
This particular View Controller is fired from the AppDelegate file upon receiving a local notificaton that I set in the app. The code for it is as below
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VCShowNotification") as? ShowNotificationVC {
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
let navController = UINavigationController(rootViewController: controller)
currentController.present(navController, animated: true, completion: nil)
//currentController.present(controller, animated: true, completion: nil)
}
}}
I've now updated the launch of the notification controller from my appdelegate by adding a 1 second delay to it and on the main thread. This now opens up the app, but the notification controller isn't showing up anymore on some devices. The app has stopped crashing though. I can't get to simulate the error on my devices, which is why I'm really struggling with this one.