title: java callback from c++ with android and sdl
body: <p>I am trying to call a java method from with android/SDL c++ code to popup or update a Toast. but I am getting</p>
  
  <pre><code>W/dalvikvm( 4026): Invalid indirect reference 0x41e6b710 in decodeIndirectRef
  </code></pre>
  
  <p>The Toast appears to work when updated from Java side (eg. from a regularly called Handler) and my message string will Log() OK, but as soon as a set anything (even a "Hello World") in the setText() I get the crash.</p>
  
  <p>my main.cc (with reference to <a href="https://wiki.libsdl.org/SDL_AndroidGetJNIEnv" rel="nofollow">https://wiki.libsdl.org/SDL_AndroidGetJNIEnv</a>)</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  #include "SDL.h"
  
  #include &lt;android/log.h&gt;
  #include &lt;jni.h&gt;
  
  #define log(...) __android_log_print(ANDROID_LOG_INFO, "SDL/c", __VA_ARGS__);
  
  void progressMessage(const char *message)
  {
      log("0) '%s'", message);
      JNIEnv *jni_env = (JNIEnv*)SDL_AndroidGetJNIEnv();
      log("1) env %08x", (unsigned int)jni_env);
      jobject jni_activity = (jobject)SDL_AndroidGetActivity();
      log("2) act %08x", (unsigned int)jni_activity);
  
      jclass jni_class(jni_env-&gt;GetObjectClass(jni_activity));
      log("3) cls %08x", (unsigned int)jni_class);
      jstring jstr = jni_env-&gt;NewStringUTF(message);
      log("4) str %08x", (unsigned int)jstr);
      jmethodID method = jni_env-&gt;GetMethodID(jni_class,
      "progressMessage", "(Ljava/lang/String;)V    ");
      log("5) met %08x", (unsigned int)method);
  
      jni_env-&gt;CallVoidMethod(jni_activity, method, jstr);
      log("6) done");
      jni_env-&gt;DeleteLocalRef(jni_activity);
      jni_env-&gt;DeleteLocalRef(jni_class);
  }
  
  int main(int argc, char *argv[])
  {
      SDL_Window *window;
      SDL_Renderer *renderer;
  
      if(SDL_CreateWindowAndRenderer(0, 0, 0, &amp;window, &amp;renderer) &lt; 0)
          exit(2);
  
      Uint8 done = 0;
      SDL_Event event;
  
      int loop = 0;
      while(!done)
      {
          if (loop % 100 == 0)
          {
              char message[256];
              sprintf(message, "Log %d", loop);
              progressMessage(message);
          }
  
          while(SDL_PollEvent(&amp;event))
          {
              if(event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_FINGERDOWN)
              {
                  done = 1;
              }
          }
  
          int col = loop % 512;
          if (col &gt; 255)
              col = 256-col;
          SDL_SetRenderDrawColor(renderer, col,col,col, 0xFF);
          SDL_RenderClear(renderer);
          SDL_RenderPresent(renderer);
          SDL_Delay(10);
  
          loop++;
      }
  
      exit(0);
  }
  </code></pre>
  
  <p>My class</p>
  
  <pre><code>package com.example.test;
  
  import android.util.Log;
  import android.widget.Toast;
  import android.os.Bundle;
  
  import org.libsdl.app.SDLActivity; 
  
  public class SDLTest extends SDLActivity {
  
      public static Toast toast = null;
  
      @Override
      public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
  
          this.progressMessage("Starting");
      }
  
      public void progressMessage(String message) {
          Log.i("SDL/java", message);
          toast.setText(message);
          toast.setDuration(Toast.LENGTH_SHORT);
          toast.show();
      }
  }
  </code></pre>
  
  <p>and here's the logcat output</p>
  
  <pre><code>I/SDL     ( 4561): SDL_Android_Init()
  I/SDL     ( 4561): SDL_Android_Init() finished!
  I/SDL/c   ( 4561): 0) 'Log 0'
  I/SDL/c   ( 4561): 1) env 76160ec8
  I/SDL/c   ( 4561): 2) act 2d900009
  I/SDL/c   ( 4561): 3) cls 1dd00011
  I/SDL/c   ( 4561): 4) str 1d200015
  I/SDL/c   ( 4561): 5) met 6d788688
  I/SDL/java( 4561): Log 0
  I/SDL/c   ( 4561): 6) done
  I/ActivityManager(  594): Displayed com.example.test/.SDLTest: +218ms
  V/SDL     ( 4561): onWindowFocusChanged(): true
  I/SDL/c   ( 4561): 0) 'Log 100'
  I/SDL/c   ( 4561): 1) env 76160ec8
  I/SDL/c   ( 4561): 2) act 1de00011
  I/SDL/c   ( 4561): 3) cls 2da00009
  I/SDL/c   ( 4561): 4) str 1dc00019
  I/SDL/c   ( 4561): 5) met 6d788688
  I/SDL/java( 4561): Log 100
  I/SDL/c   ( 4561): 6) done
  I/SDL/c   ( 4561): 0) 'Log 200'
  I/SDL/c   ( 4561): 1) env 76160ec8
  I/SDL/c   ( 4561): 2) act 41e64a80
  W/dalvikvm( 4561): Invalid indirect reference 0x41e64a80 in decodeIndirectRef
  I/dalvikvm( 4561): "SDLThread" prio=5 tid=11 RUNNABLE
  I/dalvikvm( 4561):   | group="main" sCount=0 dsCount=0 obj=0x41e75fd0 self=0x761607a0
  I/dalvikvm( 4561):   | sysTid=4579 nice=0 sched=0/0 cgrp=apps handle=1981156344
  I/dalvikvm( 4561):   | state=R schedstat=( 0 0 0 ) utm=3 stm=11 core=1
  I/dalvikvm( 4561):   at org.libsdl.app.SDLActivity.nativeInit(Native Method)
  I/dalvikvm( 4561):   at org.libsdl.app.SDLMain.run(SDLActivity.java:915)
  I/dalvikvm( 4561):   at java.lang.Thread.run(Thread.java:841)
  I/dalvikvm( 4561): 
  E/dalvikvm( 4561): VM aborting
  </code></pre>
  
  <p>It appears that the 'activity' pointer has become corrupted.</p>
  
  <p>(I'm also hoping that env, activity, class and method can all be cached)</p>
  
  <p>What have I missed?</p>