Swift malloc_error_break因双空闲而崩溃



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

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

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

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

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

相关内容

  • 没有找到相关文章

最新更新