0

I use Weakreferences for the callback in a asynctask. In the constructor of the asynctask i give a list with references. In my situation the list contains 3 references, 2 fragment references and 1 java class reference.

When i check the list which is recieved by the constructor of the asynctask the list contains the 3 (filled) references. I copy these to a local list (in the asynctask). When i check this list this has also the 3 (filled) references.

Then the asynctask is (doInBackground) is executed which don't touch the references. When i check the references with a breakpoint at the first line in the doInBackground method the 3th (java class) reference is null. The other 2 references are still filled.

Everything worked fine till tomorrow. I checked the differences in code but there are no big differences. I tryed to roll them back with no result. Has someone an explaination for this?

I created the references on this way: WeakReference<e_Alerts> wr = new WeakReference<e_Alerts>(this); callbackReferences.add(new WeakReference<>((e_Alerts)tab_AlertListOverviewFragment)); callbackReferences.add(new WeakReference<>((e_Alerts)tab_AlertMapsOverviewFragment));

And the list i used is a simple List<WeakReference<e_Alerts>> callbackReferences; list.

--------------------------------------------------------Update--------------------------------------------------------

doInBackground code:

    try {
        //Downloads the alert XMLs from the internet and parses it to xmlAlerts
        this.alerts = new XmlDownloader().DownloadAlerts(inputUrl);
        // Filters the xml alerts so only the alerts where the enduser is interessed in will remain
        this.alerts = filterAlerts(this.alerts);
        // Converts the remaining xmlAlerts to Alerts;
        this.result = new AlertConverter().Convert(this.alerts);
    }catch (Exception e) {
        Log.e("At_allAlerts",e.getMessage());
    }
    return null;

filterAlerts Method:

private List<Item> filterAlerts(List<Item> alerts) {
    List<Item> filteredXmlAlerts = new ArrayList<>();

    for (Item alert : alerts)
    {
        Location alertLocation = new Location("");
        alertLocation.setLatitude(alert.getGeometries().get(0).getLocations().get(0).getLat());
        alertLocation.setLongitude(alert.getGeometries().get(0).getLocations().get(0).getLng());

        for(Area area : this.areas)
        {
            if (area.IsOrganization() && alert.getCountryCode().toLowerCase().equals(area.getOrganizationcode().toLowerCase())){
                filteredXmlAlerts.add(alert);
                break;
            }
            else if(!area.IsOrganization() && isAlertInRegion(alertLocation, area)) {
                filteredXmlAlerts.add(alert);
                break;
            }
        }
    }
    return filteredXmlAlerts;
}

The XmlDownloader: Downloads an xml feed an parses the xml to objects with a library

The AlertConverter: converts the xml object to the object i use in my app

Both classes can work without the asynctask class and don't use the references.

CodeNinja
  • 836
  • 1
  • 15
  • 38

1 Answers1

1

The garbage collector can free/null objects without a "strong" reference. It can be the case that the variables that you use doesn't have any references left

  • Thanks for your answer but for some weird reason the problem is solved by himself. I removed the app manually from my phone (for testing first initialization) and since this moment everything works fine again. I checked the changes (in history) with the clases at the moment from this post so i can find a explaination why it was broken and fixed again but there are 0.0 changes....... I don't understand it but it works so i'am happy :-D – CodeNinja Mar 30 '16 at 12:45
  • I found something more... the problem is not "magically gone".... When i start the app the first time the gps location is asked to an gpsManager class by the alert manager. When i disable that the app gets the gps location `(if gpsEnabled){gpsManager.getGps();}` the gps class (with a Location listener) then the Alertmanager reference goes to null else not..... – CodeNinja Mar 31 '16 at 07:35