AndroidAdvanceWithGeektime / Chapter07

Sample for Add Systrace Tag
224 stars 71 forks source link

生成的trace显示com.sample.systrace.MainActivity.onCreate.(Landroid.os.Bundle;)V(Did Not Finish) #2

Open YuanYuan927 opened 4 years ago

YuanYuan927 commented 4 years ago

生成的trace显示com.sample.systrace.MainActivity.onCreate.(Landroid.os.Bundle;)V(Did Not Finish),实际上onCreate早运行完成了,我自己手动插桩就是正常的请问这是什么原因?

softrice commented 4 years ago

@YuanSun927 Trace.beginSection(name)和 Trace.endSection()不对应导致的。 起初我怀疑是多线程导致的不对应问题,加了线程限制后还是有这个问题。 后来我建立一个变量,在Trace.beginSection(name)变量+1,Trace.endSection()的时候变量-1. 最后得到的结果是6。说明有部分函数没有正确的调用 Trace.endSection()。

softrice commented 4 years ago
package android.support.graphics.drawable
public class AnimatorInflaterCompat {
    public static Animator loadAnimator(Context context, Resources resources, Theme theme, @AnimatorRes int id, float pathErrorScale) throws NotFoundException {
        TraceTag.i("android.support.graphics.drawable.AnimatorInflaterCompat.loadAnimator.");
        XmlResourceParser parser = null;

        Animator var7;
        try {
            NotFoundException rnf;
            try {
                parser = resources.getAnimation(id);
                Animator animator = createAnimatorFromXml(context, resources, theme, parser, pathErrorScale);
                var7 = animator;
            } catch (XmlPullParserException var13) {
                rnf = new NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(var13);
                TraceTag.o();
                throw rnf;
            } catch (IOException var14) {
                rnf = new NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(var14);
                TraceTag.o();
                throw rnf;
            }
        } catch (Throwable var15) {
            if (parser != null) {
                parser.close();
            }

            TraceTag.o();
            throw var15;
        }

        if (parser != null) {
            parser.close();
        }

        TraceTag.o();
        return var7;
    }
}

如这个类 可能就会造成2次调用Trace.endSection()

softrice commented 4 years ago

查看了matrix源码,解决方法是Trace.beginSection的时候传入唯一id。 像这样Trace.beginSection(name,34234224) Trace.endSection(34234224) 这样不会导致多次Trace.endSection,但是没找到没调用Trace.endSection的情况。