1

我正在开发一个从 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

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

4

1 回答 1

5

如果没有任何代码来支持您的问题,很难确切地说出是什么原因造成的。作为教育说明:指针是对在堆上分配的内存的引用,许多不同的对象可以指向相同的内存位置。尽管 Swift 并没有真正使用指针,但底层运行时确实使用了指针,在这里,您尝试访问运行时认为不应再在程序内存中分配的一些内存位置。至于汇编代码,它向您展示了发生这种情况时硬件中正在发生的事情。一个简短的概要表明,硬件正试图在寄存器之间的内存中移动对象,当它弹出以访问被寄存器中存储的内容取消引用的内存位置之一时,这就是它崩溃的地方。认为这可能正在发生,或者来自应用程序的某个部分,它经常这样做,也许我们可以提供更多帮助!

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

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

于 2016-05-05T15:47:35.770 回答