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

NativeActivity.AssetManager is nil #6

Closed
FlorianUekermann opened this issue Aug 14, 2017 · 5 comments
Closed

NativeActivity.AssetManager is nil #6

FlorianUekermann opened this issue Aug 14, 2017 · 5 comments

Comments

@FlorianUekermann
Copy link
Contributor

The AssetManager field of the android.NativeActivity returned by app.NativeActivity.NativeActivity() is nil.

I'm building for API 24 with:
export CGO_CFLAGS="-march=armv7-a"
export GOOS=android
export GOARCH=arm
export GOARM=7

The device is a Pixel XL

@xlab
Copy link
Owner

xlab commented Aug 14, 2017

Hi, by default all struct wrappers hold reference to the original struct, fields are not copied.
To copy all fields from the underlying C struct, including AssetManager reference for this case, use Deref():

a := app.NativeActivity.NativeActivity()
a.Deref()
// ... access fields

@FlorianUekermann
Copy link
Contributor Author

Thanks. I missed the Defer. However I am still having problems with the AssetManager. Maybe I misunderstood the rather sparse NDK docs. As soon as I get the app.onCreate event I do this:

app.Main(func(a app.NativeActivity) {
...
  for {
    select {
    case event := <-lifecycleEvents:
      switch event.Kind {
      case app.OnCreate:
        log.Println("onCreate")
        activity = a.NativeActivity()
        log.Println(activity == nil)
        activity.Deref()
        log.Println(activity.AssetManager == nil)
        var asset = android.AssetManagerOpen(activity.AssetManager, "textures/textures.txt", android.AssetModeStreaming)
        if asset == nil {
          panic("asset not found")
        }
...

I have verified that the apk contains "/assets/textures/textures.txt". The output I get is:

08-15 11:05:13.541  8519  8534 I inact   : onCreate
08-15 11:05:13.541  8519  8534 I inact   : false
08-15 11:05:13.541  8519  8534 I inact   : false
08-15 11:05:13.543  8519     0 E Go      : panic: asset not found
08-15 11:05:13.543  8519     0 E Go      : 
08-15 11:05:13.543  8519     0 E Go      : goroutine 7 [running, locked to thread]:
08-15 11:05:13.543  8519     0 E Go      : inact.Init.func1(0xe9bc7250, 0xe9bc44e8)
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/inact/android.go:61 +0x5b0
08-15 11:05:13.543  8519     0 E Go      : github.com/xlab/android-go/app.Main(0xe9b4ba30)
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/github.com/xlab/android-go/app/app.go:84 +0x38
08-15 11:05:13.543  8519     0 E Go      : inact.Init()
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/inact/android.go:96 +0x78
08-15 11:05:13.543  8519     0 E Go      : main.main()
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/voxelengine/main.go:28 +0x14
08-15 11:05:13.543  8519     0 E Go      : github.com/xlab/android-go/app/internal/callfn.CallFn(0xe9ac1258)
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/github.com/xlab/android-go/app/internal/callfn/callfn_arm.s:10 +0x18
08-15 11:05:13.543  8519     0 E Go      : created by github.com/xlab/android-go/app.callMain
08-15 11:05:13.543  8519     0 E Go      : 	/home/florian/go/src/github.com/xlab/android-go/app/app.go:50 +0x354

A bit of background: As you can see from the stack trace, this code is in a function (inact.Init()), which is part of an imported package. inact.Init() is the first function called in main() and the first line inside the function is runtime.LockOSThread(). Then loop over incoming lifecycle events until onCreate is received, in which case the code above is called.

Btw. I think it would be great if the app package had something like app.NativeActivity.OpenAssetReader(name string) io.Reader. I am happy to contribute that code, but I need to get it to run first :-).

@xlab
Copy link
Owner

xlab commented Aug 15, 2017

Safe strings are disabled for this package, that means the strings you pass to binding should be \x00-ended, otherwise you're feeding the whole memory block :)

Try this:

 var asset = android.AssetManagerOpen(activity.AssetManager, "textures/textures.txt\x00", android.AssetModeStreaming)

I think it would be great if the app package had something like

I agree, it could also automatically add \x00 to the path. Thanks.

@FlorianUekermann
Copy link
Contributor Author

Since #7 is merged, I'll close this issue.

@xlab
Copy link
Owner

xlab commented Aug 15, 2017

@MaVo159 I refactored the function to avoid unnecessary Go memory references into C, added mutex for activity-related operations, etc.

Please check with 3a14af7

Thanks for bringing that issue.

@xlab xlab closed this as completed Aug 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants