5

This is the line where crash occurs

offsetDuration = duration - (offsets.containsKey(freq) 
                    ? offsets.get(freq) : 0l);

The values I got by catching the Exception and dumping the variables,

long offsetDuration = 0;
long duration = 391144;
TreeMap<Long, Long> offsets = {0=4024974.0, 1036800=8588.0, 1190400=88216.0, 1267200=49763.0, 1497600=87476.0, 1574400=7469.0, 1728000=54553.0, 1958400=60512.0, 2265600=246942.0, 300000=390779.0, 422400=39945.0, 652800=55204.0, 729600=46829.0, 883200=19191.0, 960000=23888.0}
long freq = 300000;

The variable TreeMap<Long, Long> offsets is parsed from a json file by using the code below.

@NonNull
public static TreeMap<Long, Long>  getOffsets(Context context) throws CpuStateException {
    File file = getOffsetsFile(context);
    TreeMap<Long, Long> map;

    try {
        String s = Files.toString(file, Charsets.UTF_8).trim();
        Gson gson = new GsonBuilder().create();
        Type type = new TypeToken<TreeMap<Long, Long>>(){}.getType();
        map = gson.fromJson(s, type);
    } catch (IOException e) {
        throw new CpuStateException("Failed to read offsets!");
    }

    if (map == null)
        throw new CpuStateException("Failed to read offsets!");

    return map;
}

After examining the code many times, I can't identify a situation where this code can throw a

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
    at java.lang.Long.compareTo(Long.java:32)
    at java.util.TreeMap.find(TreeMap.java:277)
    at java.util.TreeMap.findByObject(TreeMap.java:351)
    at java.util.TreeMap.containsKey(TreeMap.java:182)
    at com.vibhinna.library.engine.CpuStates.getCpuData(CpuStates.java:96)
    at com.vibhinna.library.engine.CpuStates.getBarData(CpuStates.java:162)

Any ideas?

Updates 1: this is how the json is generated:

public static void offsetTimers(TreeMap<Long, Long> offsets, Context context) throws CpuStateException {
    Gson gson = new GsonBuilder().create();
    String json = gson.toJson(offsets);
    File file = getOffsetsFile(context);

    try {
        OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(file),
                "UTF-8");
        outputStream.write(json);
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        throw new CpuStateException("Failed to save offsets!");
    }
}

Update 2 : I am not even able to reproduce this even in the original app, the crash is rare and reported by about only in less than 1% of clients.

Update 3

offset class: class java.lang.String    value: 1036800
offset class: class java.lang.String    value: 1190400
offset class: class java.lang.String    value: 1267200
offset class: class java.lang.String    value: 1497600
offset class: class java.lang.String    value: 1574400
offset class: class java.lang.String    value: 1728000
offset class: class java.lang.String    value: 1958400
offset class: class java.lang.String    value: 2265600
offset class: class java.lang.String    value: 300000
offset class: class java.lang.String    value: 422400
offset class: class java.lang.String    value: 652800
offset class: class java.lang.String    value: 729600
offset class: class java.lang.String    value: 883200
offset class: class java.lang.String    value: 960000

json:

{  
   "0":256093,
   "300000":105045,
   "422400":9677,
   "652800":10443,
   "729600":8868,
   "883200":3951,
   "960000":7323,
   "1036800":18668,
   "1190400":34938,
   "1267200":17151,
   "1497600":11018,
   "1574400":1173,
   "1728000":22881,
   "1958400":21076,
   "2265600":66501
}
30
  • Please post the original JSON... are you able to reproduce this in a short but complete desktop app?
    – Jon Skeet
    Jul 28, 2015 at 6:36
  • @JonSkeet I am not even able to reproduce this even in the original app, the crash is rare and reported by about only in less than 1% of clients.
    – Binoy Babu
    Jul 28, 2015 at 6:37
  • It would be worth putting that information in the question then. I assume that means you don't know the JSON that caused this?
    – Jon Skeet
    Jul 28, 2015 at 6:39
  • 2
    Well that's one sample of the JSON. Just to try reproducing this, try specifying a really huge long value which can't be exactly represented as a double... 9223372036854775806 would do. See what JSON that produces, and what happens when you parse it.
    – Jon Skeet
    Jul 28, 2015 at 6:44
  • 1
    The stack trace indicates that a java.lang.String object is being passed down from CpuStates.getCpuData(CpuStates.java:96). I think you need to find out where that object is coming from.
    – Steve C
    Jul 28, 2015 at 7:00

1 Answer 1

0

Here's the ugly workaround that seems to be working.

@NonNull
public static TreeMap<Long, Long>  getOffsets(Context context) throws CpuStateException {
    File file = getOffsetsFile(context);
    TreeMap map;

    try {
        String s = Files.toString(file, Charsets.UTF_8).trim();
        Gson gson = new GsonBuilder().create();
        Type type = new TypeToken<TreeMap<Long, Long>>(){}.getType();
        map = gson.fromJson(s, type);
    } catch (IOException e) {
        throw new CpuStateException("Failed to read offsets!");
    }

    if (map == null)
        throw new CpuStateException("Failed to read offsets!");

    // So, I don't trust Gson. On some devices the above code seems to be producing a
    // TreeMap<String, Double/Float>. So let's assume it's either a String or a boxed primitive.
    // Even if the assumption is wrong we're no worse off than we were before.
    // The String we'll parse, the primitive we'll cast. Brace yourselves for a horrible hack!
    // TODO find a proper fix.
    Iterator it = map.keySet().iterator();
    TreeMap<Long, Long> treeMap = new TreeMap<>();
    while (it.hasNext()) {
        Object key = it.next();
        Object value = map.get(key);
        Long lKey, lValue;

        if ((key instanceof Long)) {
            lKey = (Long) key;
        } else {
            Log.e(TAG, "Invalid key type in TreeMap: " + key.getClass());
            if (key instanceof Number) {
                lKey = ((Number) key).longValue();
            } else if (key instanceof String) {
                lKey = Double.valueOf((String) key).longValue();
            } else {
                throw new IllegalArgumentException("Invalid key in TreeMap: "  + key);
            }
        }

        if ((value instanceof Long)) {
            lValue = (Long) value;
        } else {
            Log.e(TAG, "Invalid value type in TreeMap: " + value.getClass());
            if (value instanceof Number) {
                lValue = ((Number) value).longValue();
            } else if (value instanceof String) {
                lValue = Double.valueOf((String) value).longValue();
            } else {
                throw new IllegalArgumentException("Invalid value in TreeMap: " + value);
            }
        }
        treeMap.put(lKey, lValue);
    }

    return treeMap;
}
12
  • Of course this works, but it doesn't solve the real problem. Your heap is still getting corrupted and you don't know why. What version of android is this?
    – durron597
    Jul 29, 2015 at 3:27
  • @durron597 No, it doesn't. Do you think it's a problem in Gson?
    – Binoy Babu
    Jul 29, 2015 at 3:29
  • No; I looked at the Gson source pretty thoroughly today and couldn't figure it out.
    – durron597
    Jul 29, 2015 at 3:29
  • @durron597 ART/Dalvik then?
    – Binoy Babu
    Jul 29, 2015 at 3:30
  • @durron597 No. I have an alpha tester whose device seems to be consistently giving the same aberrant output. Do you want me to get this output since the fix confirms our suspicions?
    – Binoy Babu
    Jul 29, 2015 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.