我正试图通过仪器工具中的Leak Profiler来捕获内存泄漏并保留周期 . 但是,每当我单击Instruments中的记录按钮时,应用程序看起来就像启动然后崩溃一样 . 如果我从Xcode打开应用程序并将其转移到Instruments,我点击任何 UIButton
后应用程序崩溃 . 如果我从模拟器打开应用程序,我可以获得第二个屏幕,但随后它也会崩溃 . 此外,App在正常使用情况下不会崩溃 .
这是崩溃日志;
异常类型:EXC_BAD_ACCESS(SIGSEGV)异常子类型:KERN_INVALID_ADDRESS位于0x0000000000000018 VM区域信息:0x18不在任何区域 . 以下区域之前的字节:4377985000区域类型开始 - 结束[VSIZE] PRT / MAX SHRMOD区域开始时未使用的空间---> __TEXT 0000000104f2c000-0000000104fbc000 [576K] rx / rx SM = COW ... ti App Test]终止信号:分段错误:11终止原因:命名空间SIGNAL,代码0xb终止进程:exc处理程序[0]由线程触发:0已过滤的syslog:无找到线程0名称:调度队列:com.apple.main-thread线程0崩溃:0 libswiftCore.dylib 0x000000010597b2b4 0x1058cc000 717492 1 libswiftCore.dylib 0x000000010598823c 0x1058cc000 770620 2 UIFoundation 0x000000018f01fbdc attributeDictionaryHash 460 3基础0x0000000185417edc hashProbe 72 4基金会0x0000000185417e7c - [NSConcreteHashTable的getItem:] 40 5 UIFoundation 0x000000018f01fd54 [NSAttributeDictionary newWithDictionary:] 136 6基金会0x000000018542666c - [NSConcreteAttributedString initWithString:属性:] 124 7我的应用程序测试0x0000000104fbb894 0x104f2c000 587924 8我的应用测试0x0000000104fba588 0x104f2c000 583048 9我的应用测试0x0000000104fba494 0x104f2c000 582804 10我的应用测试0x0000000104fb9964 0x104f2c000 579940 11我的应用测试0x0000000104fb9830 0x104f2c000 579632 12我的应用测试0x0000000104fcb608 0x104f2c000 652808 13我的应用测试0x0000000104fccea4 0x104f2c000 659108 14我的应用测试0x0000000104fcc50c 0x104f2c000 656652 15我的应用测试0x0000000104fcc5a0 0x104f2c000 656800 16的UIKit 0x000000018df36bfc - [UIViewController中loadViewIfRequired] 1040 17的UIKit 0x000000018df367d4 - [UIViewController的视图] 28 18我的应用测试0x0000000104f85b6c 0x104f2c000 367468 19我的应用测试0x0000000104f85470 0x104f2c000 365680 20我的应用测试0x0000000104f85688 0x104f2c000 366216 21的UIKit 0x000000018df6b20c - [UIApplication的sendAction :to:from:forEvent:] 96 22 UIKit 0x000000018df6b18c - [UIControl sendAction:to:forEvent:] 80 23 UIKit 0x000000018df55f4c - [UIControl _sendActionsForEvents:withEvent:] 440 24 UIKit 0x000000018df6aa80 - [UIControl t ouchesEnded:withEvent:方法] 576 25的UIKit 0x000000018df6a5a0 - [一个UIWindow _sendTouchesForEvent:] 2544 26的UIKit 0x000000018df65a70 - [一个UIWindow的SendEvent:] 3208 27的UIKit 0x000000018df37078 - [UIApplication的的SendEvent:] 340 28的UIKit 0x000000018e876f98 dispatchPreprocessedEventFromEventQueue 2364 29的UIKit 0x000000018e879408 __handleEventQueueInternal 4760 30的UIKit 0x000000018e872574 __handleHIDEventFetcherDrain 152 31的CoreFoundation 0x0000000184ad0358 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 24 32的CoreFoundation 0x0000000184ad02d8 __CFRunLoopDoSource0 88 33的CoreFoundation 0x0000000184acfb60 __CFRunLoopDoSources0 204 34的CoreFoundation 0x0000000184acd738 __CFRunLoopRun 1048 35的CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436 36个GraphicsServices 0x000000018687ff84 GSEventRunModal 100 37的UIKit 0x000000018df9a880 UIApplicationMain 208 38我的应用测试0x0000000104f97854 0x104f2c000 440404 39 libdyld.dylib 0x000000018451256c开始4线程1:0 libsy stem_kernel.dylib 0x0000000184641dbc __workq_kernreturn 8 1 libsystem_pthread.dylib 0x0000000184753144 _pthread_wqthread 1288 2 libsystem_pthread.dylib 0x0000000184752c30 start_wqthread 4线程2:0 libsystem_pthread.dylib 0x0000000184752c2c start_wqthread 0线程3名称:com.apple.uikit.eventfetch-thread线程3:0 libsystem_kernel.dylib 0x0000000184620bc4 mach_msg_trap 8 1 libsystem_kernel.dylib 0x0000000184620a3c mach_msg 72 2的CoreFoundation 0x0000000184acfce4 CFRunLoopServiceMachPort 196 3的CoreFoundation 0x0000000184acd8b0 __CFRunLoopRun 1424 4的CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436 5基金会0x00000001854166e4 - [NSRunLoop 50916(NSRunLoop)runMode:beforeDate:] 304 6基金会0x0000000185435afc - [NSRunLoop 178940(NSRunLoop )runUntilDate:] 96 7UIKit的0x000000018eae602c - [UIEventFetcher threadMain] 136 8基金会0x0000000185517860 __NSThread__start 996 9 libsystem_pthread.dylib 0x000000018475432c _pthread_body 308 10 libsystem_pthread.dylib 0x00000001847541f8 _pthread_body 0 11 libsystem_pthread.dylib 0x0000000184752c38 thread_start 4线程4:0 libsystem_pthread.dylib 0x0000000184752c2c start_wqthread 0螺纹5名:COM . apple.NSURLConnectionLoader螺纹5:0 libsystem_kernel.dylib 0x0000000184620bc4 mach_msg_trap 8 1 libsystem_kernel.dylib 0x0000000184620a3c mach_msg 72 2的CoreFoundation 0x0000000184acfce4 CFRunLoopServiceMachPort 196 3的CoreFoundation 0x0000000184acd8b0 __CFRunLoopRun 1424 4的CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436 5 CFNetwork的0x0000000185157b40 [NSURLConnection的715584(装载机)_resourceLoadLoop:] 404 6基金会0x0000000185517860 __NSThread__start 996 7 libsystem_pthread.dylib 0x000000018475432c _pthread_body 308 8 libsystem_pthread.dylib 0x000000018475 41f8 _pthread_body 0 9 libsystem_pthread.dylib 0x0000000184752c38 thread_start 4线程0与ARM线程状态(64位)崩溃:x0:0x00000001c4227ac0 x1:0x000000018eca2db2 x2:0x000000016aecf908 x3:0x0000000000000000 x4:0x0000000000000000 x5:0x0000000000000020 x6:0x0000000185426654 x7:0x0000000000000130 x8:0x0000000000000000 X9:0x0000000000000000 X10:0x0119490101194980 X11:0x0000000001194901 X12:0x0000000001194800 X13:0x0000000000000001 X14:0x0000000000000000 X15:0x01194901011949c0 X16:0x0000000000000000 X17:0x0000000105988228 X18:0x0000000000000000 X19:0x00000001c4227ac0 X20:0x00000001c4227ac0 X21:0x00000001b5421000 X22:0x00000001c0124f60 X23:0x0000000000000000 X24:0x00000001c0200730 X25: 0x0000000000000000 x26:0x000000018eca3f46 x27:0x00000001b3bca000 x28:0x00000001c04578e0 fp:0x000000016aecf4f0 lr:0x000000010598823c sp:0x000000016aecf4e0 pc:0x000000010597b2b4 cpsr:0x20000000
我正在搜索这个月一个月,我不确定它是否是一个仪器bug . 我查了下面的帖子;
Xcode Instruments: Leaks - App crashes on launch
Instruments crashing while using Leaks instrument
xcode instruments causing app crash
编辑:另一个崩溃日志 . 在这个我打开应用程序并将内存传输到仪器工具并触摸UIView顶部打开一个新的控制器 .
日期/时间:2018-07-06 15:13:34.5980 0300发布时间:2018-07-06 15:13:19.6298 0300操作系统版本:iPhone OS 11.0(15A372)基带版本:2.00.01报告版本:104例外类型:EXC_BAD_ACCESS(SIGSEGV)异常子类型:KERN_INVALID_ADDRESS位于0x0000000000000018 VM区域信息:0x18不在任何区域 . 以下区域之前的字节:4330094568区域类型开始 - 结束[VSIZE] PRT / MAX SHRMOD区域开始时未使用的空间---> __TEXT 0000000102180000-00000001022a0000 [1152K] rx / rx SM = COW ... ti Filo测试]终止信号:分段错误:11终止原因:命名空间SIGNAL,代码0xb终止进程:exc处理程序[0]由线程触发:0已过滤的syslog:无找到线程0名称:调度队列:com.apple.main-thread线程0崩溃:0 libswiftCore.dylib 0x0000000102bdf278 0x102b30000 717432 1 libswiftCore.dylib 0x0000000102bec1b0 0x102b30000 770480 2的CoreFoundation 0x0000000184a3ec80 - [NSDictionary的allKeys] 108 3的UIKit 0x000000018e15738c - [NSDictionary的2294668(UIStringDrawingKeyCompatibility)_ui_attributesForDictionaryContainingUIStringDrawingKeys] 36 4的UIKit 0x000000018e09fde0 - [UINavigationBar的setTitleTextAttributes:] 144 5我的应用试验0x00000001021e82e8 0x102180000 426728 6我的应用测试0x00000001021e67c8 0x102180000 419784 7我的应用测试0x00 000001021e6a14 0x102180000 420372 8的UIKit 0x000000018e5c3f78 - [UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] 64 9的UIKit 0x000000018e5c84dc _UIGestureRecognizerSendTargetActions 124 10的UIKit 0x000000018e0b2dc8 _UIGestureRecognizerSendActions 320 11的UIKit 0x000000018df67748 - [UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] 732 12的UIKit 0x000000018e5b23fc _UIGestureEnvironmentUpdate 1056 13的UIKit 0x000000018e5b1f88 - [UIGestureEnvironment _deliverEvent:toGestureRecognizers: usingBlock:] 404 14的UIKit 0x000000018e5b10e4 - [UIGestureEnvironment _updateGesturesForEvent:窗口:] 276 15的UIKit 0x000000018df65a54 - [一个UIWindow的SendEvent:] 3180 16的UIKit 0x000000018df37078 - [UIApplication的的SendEvent:] 340 17的UIKit 0x000000018e876f98 dispatchPreprocessedEventFromEventQueue 2364 18的UIKit 0x000000018e879408 __handleEventQueueInternal 4760 19的UIKit 0x000000018e872574 __handleHIDEventFetcherDrain 152 20 CoreFoundation 0x0000000184ad0358__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 24 21的CoreFoundation 0x0000000184ad02d8 __CFRunLoopDoSource0 88 22的CoreFoundation 0x0000000184acfb60 __CFRunLoopDoSources0 204 23的CoreFoundation 0x0000000184acd738 __CFRunLoopRun 1048 24的CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436个25 GraphicsServices 0x000000018687ff84 GSEventRunModal 100 26的UIKit 0x000000018df9a880 UIApplicationMain 208 27我的应用测试0x00000001021facdc 0x102180000 503004 28 libdyld.dylib 0x000000018451256c开始4线程1:0 libsystem_pthread.dylib 0x0000000184752c2c start_wqthread 0线程2:0 libsystem_pthread.dylib 0x0000000184752c2c start_wqthread 0线程3:0 libsystem_pthread.dylib 0x0000000184752c2c start_wqthread 0线程4名称:com.apple.uikit.eventfetch-thread线程4:0 libsystem_kernel.dylib 0x0000000184620bc4 mach_msg_trap 8 1 libsystem_kernel.dylib 0x0000000184620a3c mach_msg 72 2 CoreFoundation 0x0000000184acfce4 CFRunLoopServiceMachPort 196 3 Cor eFoundation 0x0000000184acd8b0 __CFRunLoopRun 1424 4的CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436 5基金会0x00000001854166e4 - [NSRunLoop 50916(NSRunLoop)runMode:beforeDate:] 304 6基金会0x0000000185435afc - [NSRunLoop 178940(NSRunLoop)runUntilDate:] 96 7的UIKit 0x000000018eae602c - [UIEventFetcher threadMain] 136 8基金会0x0000000185517860 __NSThread__start 996 9 libsystem_pthread.dylib 0x000000018475432c _pthread_body 308 10 libsystem_pthread.dylib 0x00000001847541f8 _pthread_body 0 11 libsystem_pthread.dylib 0x0000000184752c38 thread_start 4主题5名:GAIThread螺纹5:0 libsystem_kernel.dylib 0x0000000184620bc4 mach_msg_trap 8 1 libsystem_kernel.dylib 0x0000000184620a3c mach_msg 72 2的CoreFoundation 0x0000000184acfce4 CFRunLoopServiceMachPort 196 3 CoreFoundation 0x0000000184acd8b0 __CFRunLoopRun 1424 4 CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436 5基础0x00000001854166e4 - [NSRunLoop 50916(NSRun)环)runMode:beforeDate:] 304 6基金会0x000000018546862c - [NSRunLoop 386604(NSRunLoop)运行] 88 7我的应用测试0x000000010225b108 0x102180000 897288 8基金会0x0000000185517860 __NSThread__start 996 9 libsystem_pthread.dylib 0x000000018475432c _pthread_body 308 10 libsystem_pthread.dylib 0x00000001847541f8 _pthread_body 0 11 libsystem_pthread . dylib 0x0000000184752c38 thread_start 4螺纹6名:com.apple.NSURLConnectionLoader螺纹6:0 libsystem_kernel.dylib 0x0000000184620bc4 mach_msg_trap 8 1 libsystem_kernel.dylib 0x0000000184620a3c mach_msg 72 2的CoreFoundation 0x0000000184acfce4 CFRunLoopServiceMachPort 196 3的CoreFoundation 0x0000000184acd8b0 __CFRunLoopRun 1424 4的CoreFoundation 0x00000001849ee2d8 CFRunLoopRunSpecific 436 5 CFNetwork的0x0000000185157b40 [NSURLConnection的715584(Loader)_resourceLoadLoop:] 404 6基础0x0000000185517860 __NSThread__start 996 7 libsystem_pthread.dylib 0x000000018475432c _pthread_body 308 8 libsystem_p thread.dylib 0x00000001847541f8 _pthread_body 0 9 libsystem_pthread.dylib 0x0000000184752c38 thread_start 4线程0与ARM线程状态(64位)崩溃:x0:0x00000001c4227c60 x1:0x000000018eca2db2 x2:0x0000000000000000 x3:0x000000010637e8d0 x4:0x0000000000000002 x5:0x0000000000000020 x6:0x00000001021e82e8 x7:0x0000000000000b70 X8:0x0000000000000000 X9:0x0000000000000000 X10:0x01446b0101446b80 X11:0x0000000001446b01 X12:0x0000000001446a00 X13:0x0000000000000001 X14:0x0000000000000000 X15:0x01446b0101446bc0 X16:0x0000000000000000 X17:0x0000000102bec19c X18:0x0000000000000000 X19:0x00000001c4227c60 X20:0x00000001c4227c60 X21:0x00000001c4227c60 X22:0x00000000000000d0 X23:0x00000000000001f0 X24: 0x0000000000000008 x25:0x00000001c4107740 x26:0x00000001c40ca6b0 x27:0x00000001c4093330 x28:0x00000001b3f8f000 fp:0x000000016dc7ce40 lr:0x0000000102bec1b0 sp:0x000000016dc7ce30 pc:0x0000000102bdf278 cpsr:0x20000000
EDIT: @FranticRock s'回答之后 . 当我删除 [UINavigationBar setTitleTextAttributes:] 时,详细信息类可以打开,但不会显示任何项目( Headers ,后退按钮或任何单元格) . 它们都是空的,我还在泄漏 .
这是控制器类,我在哪里写 [UINavigationBar setTitleTextAttributes:]
class WelcomeViewController: BaseViewController {
let bgView = UIView()
let backgroundImage = UIImageView()
let logoImage: UIImageView = {
let i = UIImageView()
i.image = UIImage(named: "full_logo")
i.contentMode = .scaleAspectFit
return i
}()
let welcomeLabel : UILabel = {
let l = UILabel()
l.font = Fonts.font.withSize(24)
l.textColor = UIColor.white
l.textAlignment = .center
l.numberOfLines = 0
l.sizeToFit()
return l
}()
let loginLbl: UILabel = {
let l = UILabel()
l.font = Fonts.font.withSize(13)
l.textColor = UIColor.white
l.textAlignment = .center
l.numberOfLines = 0
l.sizeToFit()
return l
}()
let adminBtn: UIButton = {
let b = UIButton()
b.backgroundColor = Color.Common.buttonColor
b.layer.cornerRadius = 6.0
return b
}()
let userBtn: UIButton = {
let b = UIButton()
b.backgroundColor = Color.Common.buttonColor
b.layer.cornerRadius = 6.0
return b
}()
lazy var supportedServicesView = WelcomeScreenButtonView(text: getLabelText(key: CMSKeys.CMS_LBL_SUPPORTEDSERVICES), imageName: "phone")
lazy var incidentView = WelcomeScreenButtonView(text: getLabelText(key: CMSKeys.CMS_LBL_INCIDENT), imageName: "phone")
lazy var contactUsView = WelcomeScreenButtonView(text: getLabelText(key:CMSKeys.CMS_LBL_CONTACT), imageName: "phone")
lazy var moreView = WelcomeScreenButtonView(text: getLabelText(key:CMSKeys.CMS_LBL_MORE), imageName: "wdiger")
lazy var emergencyBtn = SendButton(text: "Button")
let firstStackView = UIStackView()
let secondStackView = UIStackView()
let thirdStackView = UIStackView()
let btnStackView = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
self.setPageID()
designUI()
}
override func viewDidAppear(_ animated: Bool) {
if(AppContext.instance.comesFromURL != nil){
comesFromURL()
}
}
override func viewWillAppear(_ animated: Bool) {
if(analyticsName != nil){
self.sendDataToGA()
}
}
override func viewDidLayoutSubviews() {
bgView.anchor(self.thirdStackView.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 40, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
let margins = view.layoutMarginsGuide
thirdStackView.leadingAnchor.constraint(equalTo: margins.leadingAnchor,constant: 5).isActive = true
thirdStackView.trailingAnchor.constraint(equalTo: margins.trailingAnchor,constant: -5).isActive = true
thirdStackView.bottomAnchor.constraint(equalTo: self.emergencyBtn.topAnchor, constant: -10).isActive = true
thirdStackView.heightAnchor.constraint(equalTo: thirdStackView.widthAnchor, multiplier: 0.77).isActive = true
emergencyBtn.anchor(nil, left: self.view.leftAnchor, bottom: self.bottomLayoutGuide.topAnchor, right: self.view.rightAnchor, topConstant: 10, leftConstant: 20, bottomConstant: 20, rightConstant: 20, widthConstant: 0, heightConstant: 48)
if #available(iOS 11.0, *) {
logoImage.anchor(self.view.safeAreaLayoutGuide.topAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 40, leftConstant: 80, bottomConstant: 0, rightConstant: 80, widthConstant: 0, heightConstant: 0)
} else {
logoImage.anchor(self.view.topAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 40, leftConstant: 80, bottomConstant: 0, rightConstant: 80, widthConstant: 0, heightConstant: 0)
// Fallback on earlier versions
}
logoImage.anchorCenterXToSuperview()
let welcomeLblHeight = heightForView(text: "Welcome", font: welcomeLabel.font, width: self.view.bounds.width)
let loginLblHeight = heightForView(text: "Please Log In", font: loginLbl.font, width: self.view.bounds.width)
welcomeLabel.anchor(self.logoImage.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 0, leftConstant: 50, bottomConstant: 0, rightConstant: 50, widthConstant: 0, heightConstant: welcomeLblHeight)
loginLbl.anchor(self.welcomeLabel.bottomAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 10, leftConstant: 50, bottomConstant: 0, rightConstant: 50, widthConstant: 0, heightConstant: loginLblHeight)
btnStackView.anchor(nil, left: self.view.leftAnchor, bottom: self.thirdStackView.topAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 20, bottomConstant: 20, rightConstant: 20, widthConstant: 0, heightConstant: 50)
backgroundImage.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - self.bgView.frame.height)
}
private func designUI(){
self.view.translatesAutoresizingMaskIntoConstraints = true
backgroundImage.image = UIImage(named: "background.png")
backgroundImage.contentMode = .scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
bgView.backgroundColor = Color.Common.welcomeScreenBackgroundColor
self.view.insertSubview(bgView, at: 1)
view.addSubview(logoImage)
welcomeLabel.text = getLabelText(key: CMSKeys.CMS_LBL_WELCOME)
view.addSubview(welcomeLabel)
loginLbl.text = getLabelText(key: CMSKeys.CMS_LBL_LOGIN)
view.addSubview(loginLbl)
adminBtn.backgroundColor = Color.Common.adminGreen
adminBtn.setTitle(getLabelText(key: CMSKeys.CMS_BTN_ADMIN), for: .normal)
adminBtn.addTarget(self, action: #selector(adminBtnTapped(_:)), for: .touchUpInside)
userBtn.backgroundColor = Color.Common.userGreen
userBtn.setTitle(getLabelText(key: CMSKeys.CMS_BTN_USER), for: .normal)
userBtn.addTarget(self, action: #selector(userBtnTapped(_:)), for: .touchUpInside)
btnStackView.axis = UILayoutConstraintAxis.horizontal
btnStackView.distribution = UIStackViewDistribution.fillEqually
btnStackView.alignment = UIStackViewAlignment.fill
btnStackView.spacing = 10.0
btnStackView.addArrangedSubview(adminBtn)
btnStackView.addArrangedSubview(userBtn)
btnStackView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(btnStackView)
firstStackView.axis = UILayoutConstraintAxis.vertical
firstStackView.distribution = UIStackViewDistribution.fillEqually
firstStackView.alignment = UIStackViewAlignment.fill
firstStackView.spacing = 10.0
firstStackView.addArrangedSubview(supportedServicesView)
firstStackView.addArrangedSubview(contactUsView)
firstStackView.translatesAutoresizingMaskIntoConstraints = false
secondStackView.axis = UILayoutConstraintAxis.vertical
secondStackView.distribution = UIStackViewDistribution.fillEqually
secondStackView.alignment = UIStackViewAlignment.fill
secondStackView.spacing = 10.0
secondStackView.addArrangedSubview(incidentView)
secondStackView.addArrangedSubview(moreView)
secondStackView.translatesAutoresizingMaskIntoConstraints = false
thirdStackView.axis = UILayoutConstraintAxis.horizontal
thirdStackView.distribution = UIStackViewDistribution.fillEqually
thirdStackView.alignment = UIStackViewAlignment.fill
thirdStackView.spacing = 10.0
thirdStackView.addArrangedSubview(firstStackView)
thirdStackView.addArrangedSubview(secondStackView)
thirdStackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(thirdStackView)
//View Tap Gestures
let tapSupportedServices = UITapGestureRecognizer(target: self, action: #selector(self.tapSupportedServices(_:)))
supportedServicesView.addGestureRecognizer(tapSupportedServices)
supportedServicesView.isUserInteractionEnabled = true
let tapIncidentView = UITapGestureRecognizer(target: self, action: #selector(self.tapIncident(_:)))
incidentView.addGestureRecognizer(tapIncidentView)
incidentView.isUserInteractionEnabled = true
let tapContactUs = UITapGestureRecognizer(target: self, action: #selector(self.tapContactUs(_:)))
contactUsView.addGestureRecognizer(tapContactUs)
contactUsView.isUserInteractionEnabled = true
let tapMoreView = UITapGestureRecognizer(target: self, action: #selector(self.tapMore(_:)))
moreView.addGestureRecognizer(tapMoreView)
moreView.isUserInteractionEnabled = true
emergencyBtn.layer.cornerRadius = 6.0
emergencyBtn.addTarget(self, action: #selector(emergenyBtnTapped(_:)), for: .touchUpInside)
self.view.addSubview(emergencyBtn)
}
@objc func tapSupportedServices(_ sender: UITapGestureRecognizer) {
let linkingVC = SupportedServicesFirstViewController()
let controller = createNavController(title: "Services", viewController: linkingVC)
self.present(controller, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
private func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
//Gets a Title and a ViewController then returns a UINavigationController with attributes
private func createNavController(title: String, viewController: UIViewController) -> UINavigationController {
viewController.view.backgroundColor = UIColor(red:0.87, green:0.87, blue:0.87, alpha:1.0)
let navController = UINavigationController(rootViewController: viewController)
navController.navigationBar.topItem?.title = title
navController.navigationBar.barTintColor = Color.NavigationBar.tintColor
navController.navigationBar.tintColor = UIColor.white
//Fix: Leak
navController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: Color.NavigationBar.textColor]
return navController
}
private func removeViews(){
self.emergencyBtn.removeFromSuperview()
self.incidentView.removeFromSuperview()
self.moreView.removeFromSuperview()
self.supportedServicesView.removeFromSuperview()
self.contactUsView.removeFromSuperview()
}
deinit {
print("Welcome Deinited")
removeViews()
}
}
2 回答
在第一个堆栈跟踪中,有与以下内容相关的代码: [UINavigationBar setTitleTextAttributes:]
它试图通过在 Headers 上设置文本属性来自定义导航栏 . 你在做吗?你可以尝试评论一下,看看问题是否消失了?
技术上是什么你有不是内存泄漏 . 您正在访问已释放的对象 . 您可以做的另一件事是在您运行的方案上启用Zombie Objects,并查看是否可以获取有关哪个对象已取消分配的信息 .
现在,如果上面没有任何堆栈跟踪引导您直接编写任何代码,并且您说您已经使用了内存调试图工具并且没有找到任何对正在检查泄漏的对象的强引用,我会使用这里有一个更有创意的方法来找到问题 . 我经常使用的方法是消除 . 开始注释控制器的大部分,直到你只剩下空壳 . 每次,您都可以将代码缩减一半(日志性能),并重新测试代码的哪一部分引用已解除分配的对象 .
显然,由于正常的应用程序操作可能与XCode / Instruments内部相关,但上面的 setTitleTextAttributes 看起来很可疑,我首先会尝试缩小问题是否与导航栏有关 . 您是否对导航层次结构或导航栏UI的自定义做了一些奇怪的事情?
还要检查以前导航事件/实例在内存中是否存在同一个视图控制器的其他实例...这听起来像是一个UI对象被取消分配,然后由异步进程更新 .
同样覆盖你的deinit {} . 方法并在其中放置一个print语句,以检测所有被释放的相关对象 . 这将有助于缩小范围,以后将取消分配和访问哪个控制器 .
它是出现在所有iOS版本/设备型号上还是仅出现在特定型号上?
该问题引发了两个问题 . 第一个是为什么分析被打破,第二个是为什么你的应用程序有一个免费使用后的问题 .
@FranicRock就第二个问题提出了很好的建议 .
我对分析问题的建议是尝试一种解决方法 . 如果您的应用程序可以使用(编写时)Beta Xcode 10.0进行编译,那么您可以进入编辑模式(
Command-<
)并在“运行”操作的“诊断”选项卡中单击 Logging Malloc Stack ,并使用子选项 All Allocation and Free History . 然后你可以:在模拟器中运行您的应用程序 .
按调试器区域中的内存图按钮(第8个图标,看起来像三个圆圈连接在一起)
执行文件 - >导出内存图 .
您可以自己研究内存图,也可以将其放在像Pastebin这样的公共位置,以便其他人提供见解 . Apple有关于这种新方法的信息视频:WWDC-2018-416-Memory-Deep-Dive