Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when buffer is empty / SwiftUI #2

Open
matts9000 opened this issue Mar 13, 2023 · 2 comments
Open

Crash when buffer is empty / SwiftUI #2

matts9000 opened this issue Mar 13, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@matts9000
Copy link

macOS Version(s) Used to Build

macOS 13 Ventura

Xcode Version(s)

Xcode 14

Description

Hello,

I experience crashes with invalid NIL in the following function of Renderer.swift:

func selectBuffers(width: CGFloat) -> (MTLBuffer, MTLBuffer) {
    var level = 0
    for (minBuffer, maxBuffer) in zip(minBuffers, maxBuffers) {
        if CGFloat(minBuffer.length / MemoryLayout<Float>.size) < width {
            return (minBuffer, maxBuffer)
        }
        level += 1
    }

    **return (minBuffers.last!, maxBuffers.last!)**
}

I tried to reproduce this in debug mode, but I does not happen there - it happens only with the app compiled for run.
What I see is that in debug the drawing is much slower. So what I suspect is, that as I'm using SwiftUI redraws happen with empty buffers, which cause this.

BTW: thanks for this wonderful implementation of a waveform drawer!

Crash Logs, Screenshots or Other Attachments (if applicable)

No response

@matts9000 matts9000 added the bug Something isn't working label Mar 13, 2023
@atomhax
Copy link
Contributor

atomhax commented Jul 10, 2023

I'm not sure if it's helpful but here is a partial crash log confirming this issue. As you mentioned it's only in the wild when built for release. However it's only happening for 1 of my testers.

Thread 0 name:
Thread 0 Crashed:
0   sotomata                      	0x00000001001c0a78 Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value + 0 (<compiler-generated>:0)
1   sotomata                      	0x00000001001c0a78 Renderer.selectBuffers(width:) + 916 (Renderer.swift:76)
2   sotomata                      	0x00000001001c0bc4 Renderer.encode(to:pass:width:) + 220 (Renderer.swift:89)
3   sotomata                      	0x00000001001c0fcc Renderer.draw(in:) + 456 (Renderer.swift:128)
4   sotomata                      	0x00000001001c10b8 @objc Renderer.draw(in:) + 52 (<compiler-generated>:0)
5   MetalKit                      	0x00000001ed4dc050 -[MTKView draw] + 160 (MTKView.m:1376)
6   QuartzCore                    	0x0000000188c106f8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 412 (CALayer.mm:2521)
7   QuartzCore                    	0x0000000188c21a1c CA::Context::commit_transaction(CA::Transaction*, double, double*) + 444 (CAContextInternal.mm:2714)
8   QuartzCore                    	0x0000000188c50ff4 CA::Transaction::commit() + 648 (CATransactionInternal.mm:432)
9   QuartzCore                    	0x0000000188c3af3c CA::Transaction::flush_as_runloop_observer(bool) + 88 (CATransactionInternal.mm:940)
10  UIKitCore                     	0x0000000189c3bc04 _UIApplicationFlushCATransaction + 52 (UIApplication.m:3286)
11  UIKitCore                     	0x0000000189d8c4b0 _UIUpdateSequenceRun + 84 (_UIUpdateSequence.mm:114)
12  UIKitCore                     	0x000000018a3f0c8c schedulerStepScheduledMainSection + 144 (_UIUpdateScheduler.m:1015)
13  UIKitCore                     	0x000000018a3f01e8 runloopSourceCallback + 92 (_UIUpdateScheduler.m:1164)
14  CoreFoundation                	0x00000001877a2128 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CFRunLoop.c:1957)
15  CoreFoundation                	0x00000001877ae7b4 __CFRunLoopDoSource0 + 176 (CFRunLoop.c:2001)
16  CoreFoundation                	0x00000001877335e8 __CFRunLoopDoSources0 + 244 (CFRunLoop.c:2038)
17  CoreFoundation                	0x00000001877490d4 __CFRunLoopRun + 828 (CFRunLoop.c:2953)
18  CoreFoundation                	0x000000018774e3ec CFRunLoopRunSpecific + 612 (CFRunLoop.c:3418)
19  GraphicsServices              	0x00000001c2beb35c GSEventRunModal + 164 (GSEvent.c:2196)
20  UIKitCore                     	0x0000000189adb6e8 -[UIApplication _run] + 888 (UIApplication.m:3782)
21  UIKitCore                     	0x0000000189adb34c UIApplicationMain + 340 (UIApplication.m:5372)
22  SwiftUI                       	0x000000018b11f41c closure #1 in KitRendererCommon(_:) + 176 (UIKitApp.swift:37)
23  SwiftUI                       	0x000000018b0859bc runApp<A>(_:) + 152 (UIKitApp.swift:14)
24  SwiftUI                       	0x000000018b06fa64 static App.main() + 128 (App.swift:114)
25  sotomata                      	0x000000010001fb24 static MyApp.$main() + 24 (sotomataApp.swift:10)
26  sotomata                      	0x000000010001fb24 main + 36 (RecordingsView.swift:0)
27  dyld                          	0x00000001a6c26dec start + 2220 (dyldMain.cpp:1165)

@atomhax
Copy link
Contributor

atomhax commented Jul 10, 2023

I think this PR may fix the issue. Please review and let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants