title: Activity <App Name> has leaked ServiceConnection <ServiceConnection Name>@438030a8 that was originally bound here
body: <p>I'm working on my first Android app. I've got three activities in my app, and the user switches back and forth pretty frequently. I've also got a remote service, which handles a telnet connection. The apps need to bind to this service in order to send/receive telnet messages.</p>
  
  <p><strong><em>Edit</em></strong>
  Thank you BDLS for your informative answer. I have re-written my code in light of your clarification on the difference between using <code>bindService()</code> as a stand-alone function or after <code>startService()</code>, and I now only get the leak error message intermittently when using the back button to cycle between activities.</p>
  
  <p>My connection activity has the following <code>onCreate()</code> and <code>onDestroy()</code>:</p>
  
  <pre><code>/** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      /*
       * Initialize the ServiceConnection.  Note that this is the only place startService() is run.
       * It is also the only time bindService is run without dependency on connectStatus.
       */
      conn = new TelnetServiceConnection();
      //start the service which handles telnet
      Intent i = new Intent();
      i.setClassName( "com.wingedvictorydesign.LightfactoryRemote", "com.wingedvictorydesign.LightfactoryRemote.TelnetService" );
      startService(i);
      //bind to the service
      bindService(i, conn, 0);
  
      setContentView(R.layout.connect);
      setupConnectUI();
  
  }//end OnCreate()
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
  
      //unbind the service and null it out
      if (conn != null) {
          unbindService(conn);
          conn = null;
          }
  
      if(connectStatus == 0) {
          //stop the service
          Intent i = new Intent();
          i.setClassName( "com.wingedvictorydesign.LightfactoryRemote", "com.wingedvictorydesign.LightfactoryRemote.TelnetService" );
          stopService(i);
          Log.d("LightfactoryRemote", "Connect onDestroy() attempted to stop service");
          }
  
      Log.d("LightfactoryRemote", "Connect onDestroy()");
      }//end onDestroy()
  </code></pre>
  
  <p>So the service is started when the activity is started, and stopped when the activity is destroyed if no successful telnet connection was made (<code>connectStatus == 0</code>).  The other activities bind to the service only if a successful connection was made (<code>connectStatus == 1</code>, saved to a shared preferences).  Here is their <code>onResume()</code> and <code>onDestroy()</code>:</p>
  
  <pre><code>@Override
  protected void onResume() {
      super.onResume();
  
      //retrieve the shared preferences file, and grab the connectionStatus out of it.
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_WORLD_WRITEABLE);
      connectStatus = settings.getInt("connectStatus", 0);
  
      Log.d("LightfactoryRemote", "Focus onResume with " + connectStatus);
  
      //if a telnet connection is active, start the service and bind to it
      if (connectStatus == 1) {
          conn = new TelnetServiceConnection();
          Intent i = new Intent();
          i.setClassName("com.wingedvictorydesign.LightfactoryRemote", "com.wingedvictorydesign.LightfactoryRemote.TelnetService");
          bindService(i, conn, 0);
          //TODO write restore texview code
          }//end if
      }//end onResume
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      //unbind the service and null it out.
      if (conn != null) {
          Log.d("LightfactoryRemote", "Focus onDestroy() attempted to unbind service");
          unbindService(conn);
          conn = null;
          }
      Log.d("LightfactoryRemote", "Focus onDestroy()");
      }//end onDestroy()
  </code></pre>
  
  <p>So the binding happens in <code>onResume()</code> so that it will pick up the changed state from the connection activity, and in the <code>onDestroy()</code> function it is unbound, if necessary.</p>
  
  <p><strong><em>End Edit</em></strong>  </p>
  
  <p>But I still get the memory leak error message "Activity  has leaked ServiceConnection @438030a8 that was originally bound here" intermittently when switching activities.   What am I doing wrong?</p>
  
  <p>Thanks in advance for any tips or pointers!!!</p>
  
  <p>Full error message follows (from the revised code):</p>
  
  <pre><code>01-02 22:04:26.642: DEBUG/LightfactoryRemote(2024): Focus onStop()
  01-02 22:04:26.642: DEBUG/LightfactoryRemote(2024): Focus onDestroy() attempted to unbind service
  01-02 22:04:26.642: DEBUG/LightfactoryRemote(2024): Focus onDestroy()
  01-02 22:04:26.672: ERROR/ActivityThread(2024): Activity com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote has leaked ServiceConnection com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote$TelnetServiceConnection@439e51e8 that was originally bound here
  01-02 22:04:26.672: ERROR/ActivityThread(2024): android.app.ServiceConnectionLeaked: Activity com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote has leaked ServiceConnection com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote$TelnetServiceConnection@439e51e8 that was originally bound here
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread$PackageInfo$ServiceDispatcher.&lt;init&gt;(ActivityThread.java:927)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:822)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ApplicationContext.bindService(ApplicationContext.java:842)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.content.ContextWrapper.bindService(ContextWrapper.java:319)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote.onResume(LightfactoryRemote.java:102)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1225)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.Activity.performResume(Activity.java:3559)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2838)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2866)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread.access$2100(ActivityThread.java:116)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.os.Looper.loop(Looper.java:123)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at android.app.ActivityThread.main(ActivityThread.java:4203)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at java.lang.reflect.Method.invoke(Method.java:521)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
  01-02 22:04:26.672: ERROR/ActivityThread(2024):     at dalvik.system.NativeStart.main(Native Method)
  01-02 22:04:26.692: WARN/ActivityManager(558): Unbind failed: could not find connection for android.os.BinderProxy@43c509a8
  </code></pre>
  
  <p><strong><em>Edit the 2nd</em></strong><br>
  Thanks once again bdls for your suggestions.  I did as you suggested and added an <code>onUnBind()</code> override to the service.  <code>onUnBind()</code> is actually only triggered when all clients disconnect from the service, but when I hit the home button, it executed, then the error message popped up!  This makes no sense to me, as all the clients have been unbound from the service, so how could the one destroyed leak a serviceConnection?  Check it out:</p>
  
  <pre><code>01-03 19:38:30.837: DEBUG/LightfactoryRemote(1118): Focus onPause()1
  01-03 19:38:31.577: WARN/IInputConnectionWrapper(1118): showStatusIcon on inactive InputConnection
  01-03 19:38:31.587: DEBUG/LightfactoryRemote(1118): Focus onStop()
  01-03 19:38:31.600: DEBUG/LightfactoryRemote(1118): Focus onDestroy() attempted to unbind service
  01-03 19:38:31.607: DEBUG/LightfactoryRemote(1118): Focus onDestroy()
  01-03 19:38:31.677: DEBUG/LightfactoryRemote(1125): TelnetService onUnBind()
  01-03 19:38:31.727: ERROR/ActivityThread(1118): Activity com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote has leaked ServiceConnection com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote$TelnetServiceConnection@435baeb0 that was originally bound here
  01-03 19:38:31.727: ERROR/ActivityThread(1118): android.app.ServiceConnectionLeaked: Activity com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote has leaked ServiceConnection com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote$TelnetServiceConnection@435baeb0 that was originally bound here
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread$PackageInfo$ServiceDispatcher.&lt;init&gt;(ActivityThread.java:886)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread$PackageInfo.getServiceDispatcher(ActivityThread.java:781)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ApplicationContext.bindService(ApplicationContext.java:820)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.content.ContextWrapper.bindService(ContextWrapper.java:307)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at com.wingedvictorydesign.LightfactoryRemote.LightfactoryRemote.onResume(LightfactoryRemote.java:102)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1225)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.Activity.performResume(Activity.java:3530)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2287)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.os.Handler.dispatchMessage(Handler.java:99)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.os.Looper.loop(Looper.java:123)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at android.app.ActivityThread.main(ActivityThread.java:3948)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at java.lang.reflect.Method.invoke(Method.java:521)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
  01-03 19:38:31.727: ERROR/ActivityThread(1118):     at dalvik.system.NativeStart.main(Native Method)
  01-03 19:38:31.777: WARN/ActivityManager(564): Unbind failed: could not find connection for android.os.BinderProxy@4370f8a8
  </code></pre>
  
  <p>I thought it might be something like you said, where the binding to the service is not complete when <code>unbindService()</code> is called, however I tried calling a method on the service as I backed through each activity to verify that the binding is complete, and they all went through fine.</p>
  
  <p>In general, this behavior doesn't seem related to how long I stay in each activity.  Once the first activity leaks its serviceConnection, however, they all do as I back through them after that.</p>
  
  <p>One other thing, if I turn on "Immediately destroy activities" in Dev Tools, it prevents this error.</p>
  
  <p>Any ideas?</p>