progrium / darwinkit

Native Mac APIs for Go. Previously known as MacDriver
MIT License
4.42k stars 147 forks source link

corelocation API giving panic #243

Closed tejaskumark closed 6 months ago

tejaskumark commented 6 months ago

Hello Experts,

I was trying to build one small program with example provided and wanted to show location details of device in opened window.

Since I am yet learning new things about Mac OS, I am not able to figure out why things are giving me panic on line 22. If someone can help here that would be great help.

Panic signature

github.com/progrium/macdriver/macos/corelocation.(*LocationManagerDelegate).LocationManagerDidUpdateLocations(...)
    /Users/lab/go/pkg/mod/github.com/progrium/macdriver@v0.5.0-preview.0.20231114193140-e468a641af06/macos/corelocation/location_manager_delegate.gen.go:138
main.main()
    /Users/lab/example_macos/main.go:23 +0xa4

Below is my example code,

package main

import (
    "fmt"
    "runtime"

    "github.com/progrium/macdriver/macos/appkit"
    "github.com/progrium/macdriver/macos/corelocation"
    "github.com/progrium/macdriver/macos/foundation"
    "github.com/progrium/macdriver/objc"
)

func main() {
    runtime.LockOSThread()

    app := appkit.Application_SharedApplication()
    delegate := &appkit.ApplicationDelegate{}

    locationdelegate := &corelocation.LocationManagerDelegate{}
    locationmanager := corelocation.NewLocationManager()
    locationmanager.SetDelegate(locationdelegate)
    loc := make([]corelocation.Location, 0)
    locationdelegate.LocationManagerDidUpdateLocations(locationmanager, loc)
    locationmanager.SetDesiredAccuracy(-1.000000)
    locationmanager.RequestLocation()
    rc := locationmanager.Location()
    fmt.Println(rc.Coordinate().Longitude, rc.Coordinate().Latitude)

    delegate.SetApplicationDidFinishLaunching(func(foundation.Notification) {
        w := appkit.NewWindowWithSize(600, 400)
        objc.Retain(&w)
        w.SetTitle("Test")

        textView := appkit.TextView_ScrollableTextView()
        textView.SetTranslatesAutoresizingMaskIntoConstraints(false)
        tv := appkit.TextViewFrom(textView.DocumentView().Ptr())
        tv.SetAllowsUndo(true)
        tv.SetRichText(false)
        w.ContentView().AddSubview(textView)
        w.ContentView().LeadingAnchor().ConstraintEqualToAnchorConstant(textView.LeadingAnchor(), -10).SetActive(true)
        w.ContentView().TopAnchor().ConstraintEqualToAnchorConstant(textView.TopAnchor(), -10).SetActive(true)
        w.ContentView().TrailingAnchor().ConstraintEqualToAnchorConstant(textView.TrailingAnchor(), 10).SetActive(true)
        w.ContentView().BottomAnchor().ConstraintEqualToAnchorConstant(textView.BottomAnchor(), 10).SetActive(true)

        w.MakeKeyAndOrderFront(nil)
        w.Center()

        setSystemBar(app)

        app.SetActivationPolicy(appkit.ApplicationActivationPolicyRegular)
        app.ActivateIgnoringOtherApps(true)
    })
    delegate.SetApplicationWillFinishLaunching(func(foundation.Notification) {
        setMainMenu(app)
    })
    delegate.SetApplicationShouldTerminateAfterLastWindowClosed(func(appkit.Application) bool {
        return true
    })
    app.SetDelegate(delegate)
    locationmanager.RequestWhenInUseAuthorization()
    fmt.Println(locationmanager.AuthorizationStatus())
    app.Run()
}

func setMainMenu(app appkit.Application) {
    menu := appkit.NewMenuWithTitle("main")
    app.SetMainMenu(menu)

    mainMenuItem := appkit.NewMenuItemWithSelector("", "", objc.Selector{})
    mainMenuMenu := appkit.NewMenuWithTitle("App")
    mainMenuMenu.AddItem(appkit.NewMenuItemWithAction("Hide", "h", func(sender objc.Object) { app.Hide(nil) }))
    mainMenuMenu.AddItem(appkit.NewMenuItemWithAction("Quit", "q", func(sender objc.Object) { app.Terminate(nil) }))
    mainMenuItem.SetSubmenu(mainMenuMenu)
    menu.AddItem(mainMenuItem)

    testMenuItem := appkit.NewMenuItemWithSelector("", "", objc.Selector{})
    testMenu := appkit.NewMenuWithTitle("Edit")
    testMenu.AddItem(appkit.NewMenuItemWithSelector("Select All", "a", objc.Sel("selectAll:")))
    testMenu.AddItem(appkit.MenuItem_SeparatorItem())
    testMenu.AddItem(appkit.NewMenuItemWithSelector("Copy", "c", objc.Sel("copy:")))
    testMenu.AddItem(appkit.NewMenuItemWithSelector("Paste", "v", objc.Sel("paste:")))
    testMenu.AddItem(appkit.NewMenuItemWithSelector("Cut", "x", objc.Sel("cut:")))
    testMenu.AddItem(appkit.NewMenuItemWithSelector("Undo", "z", objc.Sel("undo:")))
    testMenu.AddItem(appkit.NewMenuItemWithSelector("Redo", "Z", objc.Sel("redo:")))
    testMenuItem.SetSubmenu(testMenu)
    menu.AddItem(testMenuItem)
}

func setSystemBar(app appkit.Application) {
    item := appkit.StatusBar_SystemStatusBar().StatusItemWithLength(appkit.VariableStatusItemLength)
    objc.Retain(&item)
    img := appkit.Image_ImageWithSystemSymbolNameAccessibilityDescription("multiply.circle.fill", "A multiply symbol inside a filled circle.")
    item.Button().SetImage(img)

    menu := appkit.NewMenuWithTitle("main")
    menu.AddItem(appkit.NewMenuItemWithAction("Hide", "h", func(sender objc.Object) { app.Hide(nil) }))
    menu.AddItem(appkit.NewMenuItemWithAction("Quit", "q", func(sender objc.Object) { app.Terminate(nil) }))
    item.SetMenu(menu)
}
progrium commented 6 months ago

Why are you calling LocationManagerDidUpdateLocations on your delegate?

tejaskumark commented 6 months ago

@progrium Thanks for the reply.

If I don't call that method then it panics with different signature mentioning LocationManagerDidUpdateLocations must have to be called.

progrium commented 6 months ago

You wanna share that?

tejaskumark commented 6 months ago

Sorry, I should have shared that earlier. Here are the traces

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001893d0570 __exceptionPreprocess + 176
    1   libobjc.A.dylib                     0x0000000188ec1eb4 objc_exception_throw + 60
    2   Foundation                          0x000000018a51843c -[NSCalendarDate initWithCoder:] + 0
    3   CoreLocation                        0x0000000193295380 CLClientStopVehicleHeadingUpdates + 61184
    4   libffi.dylib                        0x0000000199df2050 ffi_call_SYSV + 80
    5   libffi.dylib                        0x0000000199dfaadc ffi_call_int + 1208
    6   macd                                0x0000000104ed993c runtime.asmcgocall.abi0 + 124
)
libc++abi: terminating due to uncaught exception of type NSException
SIGABRT: abort
PC=0x1892460dc m=0 sigcode=0
signal arrived during cgo execution

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x1053e4930, 0x1400014dce8)
    /usr/local/go/src/runtime/cgocall.go:157 +0x54 fp=0x1400014dcb0 sp=0x1400014dc70 pc=0x104e7b674
github.com/progrium/macdriver/objc/ffi._Cfunc_ffi_call0(0x14000114ff0, 0x19329509c, 0x105d41400, 0x14000156620)
    _cgo_gotypes.go:149 +0x34 fp=0x1400014dce0 sp=0x1400014dcb0 pc=0x104f432a4
github.com/progrium/macdriver/objc/ffi.Call.func1(0x14000114ff0?, 0x200000001?, 0x199dfb2e8?, {0x14000156620?, 0x100000000?, 0x14000114ff0?})
    /Users/test/go/pkg/mod/github.com/progrium/macdriver@v0.5.0-preview.0.20231114193140-e468a641af06/objc/ffi/ffi.go:75 +0x6c fp=0x1400014dd30 sp=0x1400014dce0 pc=0x104f4379c
github.com/progrium/macdriver/objc/ffi.Call(0x14000114ff0, 0x14000156630?, 0x105d41400, {0x14000156620, 0x2, 0x2})
    /Users/test/go/pkg/mod/github.com/progrium/macdriver@v0.5.0-preview.0.20231114193140-e468a641af06/objc/ffi/ffi.go:75 +0x30 fp=0x1400014dd90 sp=0x1400014dd30 pc=0x104f436d0
github.com/progrium/macdriver/objc.Call[...]({0x1055f32e0, 0x60000303cb60}, {0x1d87d6e23}, {0x0, 0x0, 0x1054decc0})
    /Users/test/go/pkg/mod/github.com/progrium/macdriver@v0.5.0-preview.0.20231114193140-e468a641af06/objc/call.go:61 +0x518 fp=0x1400014de80 sp=0x1400014dd90 pc=0x104f5a648
github.com/progrium/macdriver/macos/corelocation.LocationManager.RequestLocation({{0x140000021a0?}})
    /Users/test/go/pkg/mod/github.com/progrium/macdriver@v0.5.0-preview.0.20231114193140-e468a641af06/macos/corelocation/location_manager.gen.go:257 +0x54 fp=0x1400014ded0 sp=0x1400014de80 pc=0x1053d2544
main.main()
    /Users/test/example_macos/main.go:25 +0x98 fp=0x1400014df70 sp=0x1400014ded0 pc=0x1053e1848
runtime.main()
    /usr/local/go/src/runtime/proc.go:250 +0x248 fp=0x1400014dfd0 sp=0x1400014df70 pc=0x104eacbf8
runtime.goexit()
    /usr/local/go/src/runtime/asm_arm64.s:1172 +0x4 fp=0x1400014dfd0 sp=0x1400014dfd0 pc=0x104ed9b14

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
    /usr/local/go/src/runtime/proc.go:381 +0xe4 fp=0x14000046fa0 sp=0x14000046f80 pc=0x104ead024
runtime.goparkunlock(...)
    /usr/local/go/src/runtime/proc.go:387
runtime.forcegchelper()
    /usr/local/go/src/runtime/proc.go:305 +0xb8 fp=0x14000046fd0 sp=0x14000046fa0 pc=0x104eace68
runtime.goexit()
    /usr/local/go/src/runtime/asm_arm64.s:1172 +0x4 fp=0x14000046fd0 sp=0x14000046fd0 pc=0x104ed9b14
created by runtime.init.6
    /usr/local/go/src/runtime/proc.go:293 +0x24

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
    /usr/local/go/src/runtime/proc.go:381 +0xe4 fp=0x14000047760 sp=0x14000047740 pc=0x104ead024
runtime.goparkunlock(...)
    /usr/local/go/src/runtime/proc.go:387
runtime.bgsweep(0x0?)
    /usr/local/go/src/runtime/mgcsweep.go:278 +0xa4 fp=0x140000477b0 sp=0x14000047760 pc=0x104e9a204
runtime.gcenable.func1()
    /usr/local/go/src/runtime/mgc.go:178 +0x28 fp=0x140000477d0 sp=0x140000477b0 pc=0x104e8ed28
runtime.goexit()
    /usr/local/go/src/runtime/asm_arm64.s:1172 +0x4 fp=0x140000477d0 sp=0x140000477d0 pc=0x104ed9b14
created by runtime.gcenable
    /usr/local/go/src/runtime/mgc.go:178 +0x74

goroutine 4 [GC scavenge wait]:
runtime.gopark(0x1400006e000?, 0x105449580?, 0x1?, 0x0?, 0x0?)
    /usr/local/go/src/runtime/proc.go:381 +0xe4 fp=0x14000047f50 sp=0x14000047f30 pc=0x104ead024
runtime.goparkunlock(...)
    /usr/local/go/src/runtime/proc.go:387
runtime.(*scavengerState).park(0x105d0d8a0)
    /usr/local/go/src/runtime/mgcscavenge.go:400 +0x5c fp=0x14000047f80 sp=0x14000047f50 pc=0x104e9808c
runtime.bgscavenge(0x0?)
    /usr/local/go/src/runtime/mgcscavenge.go:628 +0x44 fp=0x14000047fb0 sp=0x14000047f80 pc=0x104e98604
runtime.gcenable.func2()
    /usr/local/go/src/runtime/mgc.go:179 +0x28 fp=0x14000047fd0 sp=0x14000047fb0 pc=0x104e8ecc8
runtime.goexit()
    /usr/local/go/src/runtime/asm_arm64.s:1172 +0x4 fp=0x14000047fd0 sp=0x14000047fd0 pc=0x104ed9b14
created by runtime.gcenable
    /usr/local/go/src/runtime/mgc.go:179 +0xb8

goroutine 18 [finalizer wait]:
runtime.gopark(0x140000465a8?, 0x104e845f0?, 0xa0?, 0x1b?, 0x10615ca90?)
    /usr/local/go/src/runtime/proc.go:381 +0xe4 fp=0x14000046580 sp=0x14000046560 pc=0x104ead024
runtime.runfinq()
    /usr/local/go/src/runtime/mfinal.go:193 +0x10c fp=0x140000467d0 sp=0x14000046580 pc=0x104e8ddbc
runtime.goexit()
    /usr/local/go/src/runtime/asm_arm64.s:1172 +0x4 fp=0x140000467d0 sp=0x140000467d0 pc=0x104ed9b14
created by runtime.createfing
    /usr/local/go/src/runtime/mfinal.go:163 +0x84

r0      0x0
r1      0x0
r2      0x0
r3      0x0
r4      0x189239377
r5      0x16af89d60
r6      0x6e
r7      0x880
r8      0xb51a6e7232b2cde
r9      0xb51a6e6fc8efcde
r10     0x200
r11     0xb
r12     0x0
r13     0x1ff800
r14     0x7fb
r15     0x8363900e
r16     0x148
r17     0x1e8acf340
r18     0x0
r19     0x6
r20     0x1dfa5d000
r21     0x103
r22     0x1dfa5d0e0
r23     0x1e3c5a808
r24     0x1dea3b000
r25     0x0
r26     0x2
r27     0x2
r28     0x140001283d8
r29     0x16af89cd0
lr      0x18927dcc0
sp      0x16af89cb0
pc      0x1892460dc
fault   0x1892460dc
progrium commented 6 months ago

But it's saying you need to implement it. locationdelegate.SetLocationManagerDidUpdateLocations(...)

tejaskumark commented 6 months ago

Thanks a lot, was able to make it work.