Update thread continues running? [Solved]

Problems with Canvas, OpenGL, etc...

Update thread continues running? [Solved]

Postby RedToasty » Mon Jun 21, 2010 9:26 pm

Hi everyone, newbie to the forum here. My shiny Galaxy S arrived on Friday so I've just started fiddling properly. Sadly I've managed to get myself a bit stuck.

I've made a litte game using the GLSurfaceView from the SpriteMethodTest example, from my main activity I'm calling GLSurfaceView.setEvent(new UpdateThread); to set this as the "GLThread" and fire it off.

Code: Select all
    public void onCreate(Bundle savedInstanceState)
    {
      GLSurfaceView view = new GLSurfaceView(this);
      GLRenderer renderer = new GLRenderer(this);

         view.setRenderer(renderer);
         setContentView(view);

         // my object pool code gets set up here, a few objects get added which will get rendered and updated later

         view.setEvent(new UpdateThread());
    }


All works well, infact I've had a few interesting non-thread safe bugs I've bumped into to confirm it's definitely splitting the updating/rendering correctly. The issue comes from when I close the application with the back button curiously, if I then go to the applications menu and load it again, it seems the runnable UpdateThread() is getting run twice!

The thread triggers an enemy to spawn every 2 seconds, after the restart I've often got 2 spawning in that 2 second window, close and reopen a few more times and I get dozens spawning. It liteally goes through onCreate so t obviously thinks it's previously dead.

On closing it does go through:

Code: Select all
Activity.onPause/onStop/onDestroy


which fires off:

Code: Select all
mGLThread.requestExitAndWait()


which in turn successfully sets mDone to true on the thread, getting it out of it's while() loop. I'm baffled, should I be forcibly calling "stop" on the runnable or something? The implementation seems exactly the same as the demo. I can forcibly kill the thread from Taskiller or similar and it restarts fine the next time, it just doesn't seem to be shutting down tidily.

Failing that does anyone know of any good render/update threading examples? I'm not even convinced setEvent is the best way for me to go, it seems to just pass all the messages via a bulky class in the first place.

Thanks! :)
Last edited by RedToasty on Tue Jun 22, 2010 7:16 am, edited 1 time in total.
RedToasty
Freshman
Freshman
 
Posts: 9
Joined: Thu Jun 17, 2010 7:27 pm

Top

Re: Update thread continues running?

Postby RedToasty » Tue Jun 22, 2010 12:27 am

Infact looking a bit deeper, all my programs seem to do this, this might be simpler than I thought. Even my original "HelloAndroid":

Code: Select all
public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       Button button = (Button) findViewById(R.id.Button01);
       
       button.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
             Toast.makeText(HelloAndroid.this, "Hello World", Toast.LENGTH_SHORT).show();                    
          }     
       }); 
   }
}


On closing, this will leave the process still sat in memory!? Is this some kind of flag somewhere? SpriteMethodTest seems to properly delete itself, is this purely because it's using more memory?

Thanks!
RedToasty
Freshman
Freshman
 
Posts: 9
Joined: Thu Jun 17, 2010 7:27 pm

Re: Update thread continues running?

Postby RedToasty » Tue Jun 22, 2010 7:14 am

Ah, I fixed it eventually!

The issue was my static singleton object list surviving through being destroyed and reopened. First time through it was adding a game level and firing off the threads, I'd shut everything down correctly, but then reload while the process was still sat in memory. On the second run it would add the game level to the stack again, meaning I effectively had 2 levels updating on top of each other, 2 pools of enemies spawning.

Simply clearing the object list sorted this.
RedToasty
Freshman
Freshman
 
Posts: 9
Joined: Thu Jun 17, 2010 7:27 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 3 guests