Skip to content

Commit

Permalink
Memory Leak hunting, fixed a memory leak where the service connection…
Browse files Browse the repository at this point in the history
… was not unbinded
  • Loading branch information
christianrowlands committed Feb 22, 2024
1 parent 6a24db9 commit 34c5395
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
*/
public abstract class AConnectionFragment<T extends AConnectionFragment.ServiceBinder> extends Fragment implements IConnectionStateListener
{
protected final SurveyServiceConnection surveyServiceConnection = new SurveyServiceConnection();

private static final int ACCESS_PERMISSION_REQUEST_ID = 10;

private final Handler uiThreadHandler;
Expand Down Expand Up @@ -254,7 +256,19 @@ public void onResume()
@Override
public void onPause()
{
if (service != null) service.unregisterMqttConnectionStateListener(this);
try
{
requireContext().getApplicationContext().unbindService(surveyServiceConnection);
} catch (Throwable t)
{
Timber.e(t, "Could not unbind the service because it is not bound.");
}

if (service != null)
{
service.unregisterMqttConnectionStateListener(this);
service = null;
}

super.onPause();
}
Expand All @@ -263,6 +277,10 @@ public void onPause()
public void onDestroyView()
{
hideSoftInputFromWindow();

applicationContext = null;
service = null;

super.onDestroyView();
}

Expand Down Expand Up @@ -757,7 +775,6 @@ private void startAndBindToService()
applicationContext.startService(serviceIntent);

// Bind to the service
ServiceConnection surveyServiceConnection = new SurveyServiceConnection();
final boolean bound = applicationContext.bindService(serviceIntent, surveyServiceConnection, Context.BIND_ABOVE_CLIENT);
Timber.i("%s service bound in the AConnectionFragment: %s", getServiceClass().getSimpleName(), bound);
} catch (Exception e)
Expand Down

0 comments on commit 34c5395

Please sign in to comment.