gpt4 book ai didi

ios - Swift malloc_error_break 因双重释放而崩溃

转载 作者:行者123 更新时间:2023-11-28 11:06:54 24 4
gpt4 key购买 nike

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

请不要评判我,但我不知道指针是什么,释放某些东西或分配对象意味着什么。如果有人想给我快速速成类(class)和/或告诉我我的错误可能是什么,那就太好了。

编辑:我相信错误可能来自用于下载图像的 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!

关于ios - Swift malloc_error_break 因双重释放而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com