Skip to content

x/mobile/app: app.State is nil in package init #10031

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

Closed
rakyll opened this issue Feb 27, 2015 · 5 comments
Closed

x/mobile/app: app.State is nil in package init #10031

rakyll opened this issue Feb 27, 2015 · 5 comments

Comments

@rakyll
Copy link
Contributor

rakyll commented Feb 27, 2015

My Android application panics with the following error message, if I try to access app.State from a package's init.

E/Go      (18104): panic: interface conversion: interface is nil, not interface { AndroidContext() unsafe.Pointer; JavaVM() unsafe.Pointer }
E/Go      (18104): goroutine 1 [running]:
E/Go      (18104): golang.org/x/mobile/audio/al.init·1()
E/Go      (18104):  /Users/jbd/src/golang.org/x/mobile/audio/al/al_android.go:245 +0x50
E/Go      (18104): golang.org/x/mobile/audio/al.init()
E/Go      (18104):  golang.org/x/mobile/audio/al/_obj/_cgo_import.go:1 +0x3ac
E/Go      (18104): main.init()
E/Go      (18104):  /Users/jbd/src/golang.org/x/mobile/example/basic/main.go:119 +0x70
E/Go      (18104): goroutine 17 [syscall, locked to thread]:
E/Go      (18104): runtime.goexit()
E/Go      (18104):  /Users/jbd/pkg/gomobile/android-ndk-r10d/gomobile-init-619773321/go/src/runtime/asm_arm.s:1347 +0x4
E/Go      (18104): goroutine 4 [runnable]:
E/Go      (18104): golang.org/x/mobile/app.lineLog(0x93c081d0, 0x6)
E/Go      (18104):  /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:41
E/Go      (18104): created by golang.org/x/mobile/app.init·1
E/Go      (18104):  /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:69 +0x108
E/Go      (18104): goroutine 5 [runnable]:
E/Go      (18104): golang.org/x/mobile/app.lineLog(0x93c081e0, 0x4)
E/Go      (18104):  /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:41
E/Go      (18104): created by golang.org/x/mobile/app.init·1
E/Go      (18104):  /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:76 +0x194

The package causes the panic does the following:

func init() {
    state := app.State.(interface {
        JavaVM() unsafe.Pointer
        AndroidContext() unsafe.Pointer
    })
    // ...
}
@hyangah
Copy link
Contributor

hyangah commented Feb 27, 2015

// State is global application-specific state.
// ...
// State is not valid until Run has been called.
var State interface{}

2015-02-27 14:38 GMT-05:00 Burcu Dogan notifications@github.com:

My Android application panics with the following error message, if I try
to access app.State from a package's init.

E/Go (18104): panic: interface conversion: interface is nil, not interface { AndroidContext() unsafe.Pointer; JavaVM() unsafe.Pointer }
E/Go (18104): goroutine 1 [running]:
E/Go (18104): golang.org/x/mobile/audio/al.init·1()
E/Go http://golang.org/x/mobile/audio/al.init%C2%B71()%0AE/Go (18104): /Users/jbd/src/golang.org/x/mobile/audio/al/al_android.go:245 +0x50
E/Go (18104): golang.org/x/mobile/audio/al.init()
E/Go http://golang.org/x/mobile/audio/al.init()E/Go (18104): golang.org/x/mobile/audio/al/_obj/_cgo_import.go:1 +0x3ac
E/Go (18104): main.init()
E/Go (18104): /Users/jbd/src/golang.org/x/mobile/example/basic/main.go:119 +0x70
E/Go (18104): goroutine 17 [syscall, locked to thread]:
E/Go (18104): runtime.goexit()
E/Go (18104): /Users/jbd/pkg/gomobile/android-ndk-r10d/gomobile-init-619773321/go/src/runtime/asm_arm.s:1347 +0x4
E/Go (18104): goroutine 4 [runnable]:
E/Go (18104): golang.org/x/mobile/app.lineLog(0x93c081d0, 0x6)
E/Go (18104): /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:41
E/Go http://golang.org/x/mobile/app/stdio_android.go:41E/Go (18104): created by golang.org/x/mobile/app.init·1
E/Go http://golang.org/x/mobile/app.init%C2%B71%0AE/Go (18104): /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:69 +0x108
E/Go (18104): goroutine 5 [runnable]:
E/Go (18104): golang.org/x/mobile/app.lineLog(0x93c081e0, 0x4)
E/Go (18104): /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:41
E/Go http://golang.org/x/mobile/app/stdio_android.go:41E/Go (18104): created by golang.org/x/mobile/app.init·1
E/Go http://golang.org/x/mobile/app.init%C2%B71%0AE/Go (18104): /Users/jbd/src/golang.org/x/mobile/app/stdio_android.go:76 +0x194

The package causes the panic does the following:

func init() {
state := app.State.(interface {
JavaVM() unsafe.Pointer
AndroidContext() unsafe.Pointer
})
// ...
}


Reply to this email directly or view it on GitHub
#10031.

__

@rakyll
Copy link
Contributor Author

rakyll commented Feb 27, 2015

But we know about the current VM and the application context on JNI_Onload. We don't have to wait for app.Run to set the State.

@crawshaw
Copy link
Member

We do in Java. I intended State to eventually provide access to some equivalent context on the iOS side, where the initialization order is reversed: Go is the entry point, init runs, then main starts and it is not until app.Run that we initialize UIKit.

The other less important but relevant thought I had on this is packages should work hard to minimize the init work they do in apps. Apps should start fast. I suspect initialization of almost all packages (including audio) should go into an internal sync.Once on first use.

@rakyll
Copy link
Contributor Author

rakyll commented Mar 2, 2015

We can keep documenting that the State will be available after app.Run but can set the State resolver earlier and use these values internally if the init is fast enough and not affecting the startup performance. It is an internal implementation detail. If you fear that it will be abused by the users, I'm fine with doing to a sync.Once for the initialization of the audio package.

@rakyll
Copy link
Contributor Author

rakyll commented Mar 27, 2015

In favor of accessing app.State after Run is invoked, I am closing the issue.

@rakyll rakyll closed this as completed Mar 27, 2015
@mikioh mikioh changed the title mobile/app: app.State is nil in package init x/mobile/app: app.State is nil in package init Aug 5, 2015
@golang golang locked and limited conversation to collaborators Aug 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants