Swift malloc_error_break双重崩溃崩溃

时间:2016-05-05 15:12:22

标签: ios swift pointers malloc double-free

我正在开发一个从Web API获取数据的应用程序。大部分时间,它运行完美;但是,有时我会收到错误:

malloc: *** error for object 0x7fc2b061de30: double free

***在malloc_error_break中设置断点以进行调试

我设置了一个malloc_error_break断点,这会在应用崩溃时显示,第一行突出显示:

0x112375760 <+0>:  pushq  %rbp
0x112375761 <+1>:  movq   %rsp, %rbp
0x112375764 <+4>:  nop    
0x112375765 <+5>:  nopl   (%rax)
0x112375769 <+9>:  popq   %rbp
0x11237576a <+10>: retq   

我不知道这意味着什么。我已经搜索了堆栈交换几个小时,但我无法得到任何帮助。一次,错误显示为:

pointer being freed was not allocated

请不要评判我,但我不知道指针是什么,释放什么意味着什么,或分配一个对象。如果有人想给我一个快速的速成课程和/或启发我的错误,那就太好了。

编辑: 我相信错误可能来自UIImageView扩展程序以下载图像:

extension UIImageView {
func downloadedFrom(targetURL: NSURL, onCompletion: (UIImage?, NSError?) -> Void) {
    print("downloading image")
    SpotifyAPIManager.sharedInstance.auth.client.get(targetURL.absoluteString, success: { (data, response) in
        print("successfully downloaded image")
        guard (response as NSHTTPURLResponse).statusCode == 200 else  {
            print("error in http response. status code: \((response as NSHTTPURLResponse).statusCode)")
            return
        }

        let image = UIImage(data: data)
        onCompletion(image, nil)
        }, failure: { error in
            print("error while downloading image: \(error)")
    })
}

}

(auth.client.get是来自OAuthSwift框架的方法)

我在崩溃后打印出了回溯:

* thread #1: tid = 0x2cbf0, 0x00000001089aa760 libsystem_malloc.dylib`malloc_error_break, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x00000001089aa760 libsystem_malloc.dylib`malloc_error_break
frame #1: 0x00000001089a3270 libsystem_malloc.dylib`szone_error + 406
frame #2: 0x00000001089a4f07 libsystem_malloc.dylib`tiny_malloc_from_free_list + 881
frame #3: 0x00000001089a3685 libsystem_malloc.dylib`szone_malloc_should_clear + 294
frame #4: 0x00000001089ab6e6 libsystem_malloc.dylib`malloc_zone_calloc + 78
frame #5: 0x00000001089abe4f libsystem_malloc.dylib`calloc + 49
frame #6: 0x0000000109bb5ee0 ImageIO`initImageAppleJPEG + 971
frame #7: 0x0000000109bbbc9e ImageIO`_CGImagePluginInitAppleJPEG + 76
frame #8: 0x0000000109af7dff ImageIO`makeImagePlus + 1377
frame #9: 0x0000000109af7282 ImageIO`CGImageSourceCreateImageAtIndex + 184
frame #10: 0x0000000106915ef0 UIKit`_UIImageRefFromData + 423
frame #11: 0x00000001066f5086 UIKit`-[UIImage(UIImagePrivate) _initWithData:preserveScale:cache:] + 124
frame #12: 0x00000001042c7874 Spotify`@nonobjc UIImage.init(data : NSData) -> UIImage? + 36 at SongTableViewCell.swift:0
frame #13: 0x00000001042c73dc Spotify`UIImage.__allocating_init(data : NSData) -> UIImage? + 76 at SongTableViewCell.swift:0
frame #14: 0x00000001042c718b Spotify`UIImageView.(data=142201 bytes, response=0x00007fd62a64bd90, onCompletion=0x00000001042c6ab0 Spotify`partial apply forwarder for reabstraction thunk helper from @callee_unowned @convention(block) (@unowned Swift.Optional<__ObjC.UIImage>, @unowned Swift.Optional<__ObjC.NSError>) -> (@unowned ()) to @callee_owned (@owned Swift.Optional<__ObjC.UIImage>, @owned Swift.Optional<__ObjC.NSError>) -> (@unowned ()) at SongTableViewCell.swift) -> ()) -> ()).(closure #1) + 315 at SongTableViewCell.swift:65
frame #15: 0x00000001042bec63 Spotify`thunk + 35 at SpotifyAPIManager.swift:0
frame #16: 0x00000001042c6891 Spotify`partial apply for thunk + 81 at SongTableViewCell.swift:0
frame #17: 0x0000000108087f86 OAuthSwift`OAuthSwiftHTTPRequest.(data=142201 bytes, response=0x00007fd62a64bd90, error=nil, self=0x00007fd62a636411) -> ()).(closure #1).(closure #1) + 5206 at OAuthSwiftHTTPRequest.swift:149
frame #18: 0x0000000108088ae7 OAuthSwift`thunk + 103 at OAuthSwiftHTTPRequest.swift:0
frame #19: 0x0000000104bebb49 CFNetwork`__75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
frame #20: 0x0000000104bfe0f2 CFNetwork`__49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 302
frame #21: 0x000000010602b630 Foundation`__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
frame #22: 0x0000000105f66805 Foundation`-[NSBlockOperation main] + 101
frame #23: 0x0000000105f49725 Foundation`-[__NSOperationInternal _start:] + 646
frame #24: 0x0000000105f49336 Foundation`__NSOQSchedule_f + 194
frame #25: 0x00000001087b13eb libdispatch.dylib`_dispatch_client_callout + 8
frame #26: 0x00000001087991ef libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1738
frame #27: 0x00000001054430f9 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
frame #28: 0x0000000105404b99 CoreFoundation`__CFRunLoopRun + 2073
frame #29: 0x00000001054040f8 CoreFoundation`CFRunLoopRunSpecific + 488
frame #30: 0x0000000109fdead2 GraphicsServices`GSEventRunModal + 161
frame #31: 0x0000000106693f09 UIKit`UIApplicationMain + 171
frame #32: 0x00000001042cbe72 Spotify`main + 114 at AppDelegate.swift:14
frame #33: 0x00000001087e592d libdyld.dylib`start + 1

我再一次不知道这意味着什么,但它可以帮助调试。

1 个答案:

答案 0 :(得分:3)

如果没有任何代码来备份您的问题,很难确切地说出导致此问题的原因。作为教育记录:指针是对堆上分配的内存的引用,许多不同的对象可以指向相同的内存位置。虽然Swift并不真正使用指针,但是底层运行时确实如此,并且在这里,您已经尝试访问运行时认为不应再在程序内存中分配的某个内存位置。至于汇编代码,它显示了当时硬件发生了什么。简要说明硬件正在尝试在寄存器之间的内存中移动对象,当它进入弹出窗口以访问存储在寄存器中的内容取消引用的其中一个内存位置时,这就是它崩溃的地方。如果您可以发布一些您可能正在发生的思考的代码,或者从应用程序的某个部分经常发布这些代码,也许我们可以提供更多帮助!

编辑:还有实际的错误消息本身:这意味着已经释放了特定的内存位置(在这种情况下,这意味着运行时不会认为你有任何对任何对象的引用)指向那里,然后它再次尝试释放它,导致双重自由错误。

免责声明:我知道这不是一个真正的答案,并且更适合作为评论,但它太长了!我想教育OP!