简体   繁体   中英

Why is adding and removing a fragment giving me memory leak?

I am fairly new to android dev so any help is really appreciated! I am getting this error

12-30 08:14:32.391 1726-1741/? E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. java.lang.Throwable: Explicit termination method 'end' not called at dalvik.system.CloseGuard.open(CloseGuard.java:180) at java.util.zip.Inflater.(Inflater.java:82) at com.android.okhttp.okio.GzipSource.(GzipSource.java:62) at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:645) at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:827) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497) at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java) at com.google.android.gms.http.GoogleHttpClient.a(SourceFile:811) at com.google.android.gms.http .GoogleHttpClient.a(SourceFile:776) at com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:676) at com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:660) at com.google.android.gms.auth.be.ja(SourceFile:220) at com.google.android.gms.auth.be.appcert.aa(SourceFile:263) at com.google.android.gms.auth.be.appcert.aa(SourceFile:132) at com.google.android.gms.auth.be.appcert.ba(SourceFile:43) at com.google.android.gms.auth.bba(SourceFile:62) at com.google.android.gms.auth.baa(SourceFile:120) at com.google.android.gms.auth.baa(SourceFile:61) at com.google.android.gms.auth.be.cron.AuthCronService.a(SourceFile:44) at com.google.android.gms.gcm.al.run(SourceFile:135)

and part of the code that's causing the error

 gameLayout.setOnTouchListener(
            new FrameLayout.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //add bullet upon click
                    bullet_fragment b_fragment = new bullet_fragment();
                    fragmentTransaction.add(R.id.game, b_fragment).commit();

                    //bullet moves around screen (some code here)

                    // removes bullet
                    fragmentTransaction.remove(b_fragment).commit();
                    return true;
                }
          g  }
    );

so every time the screen is touched a fragment is generated so it's possible for there to be multiples of the same fragment to exist on the screen, which is no problem I've tried searching a bit and found that remove() doesn't necessarily move the fragment to the GC, but the person didn't say how to completely destroy it. How can I do that then? or is there another reason for that error to occur?

code of the fragment

public class bullet_fragment extends Fragment {
View bulletView;

ImageView bulletObject;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    bulletView = inflater.inflate(R.layout.bullet_layout, container, true);
    bulletObject = (ImageView) bulletView.findViewById(R.id.bullet);

    return bulletView;

}
public void shoot(int x, int y){
    FrameLayout.LayoutParams b_params = new FrameLayout.LayoutParams(bulletObject.getLayoutParams());


    final int B_WIDTH= (int) (bulletObject.getWidth() * 0.5);
    final int B_HEIGHT= (int) (bulletObject.getHeight() * 0.5);
    b_params.setMargins(x - B_WIDTH,y - B_HEIGHT,0,0);
    bulletObject.setLayoutParams(b_params);
}

}

The issue in the stacktrace is clearly caused by okhttp but it may not necessarily be the fault of okhttp. The fact that this happens inside gms which is part of Google Services suggests that you likely have nothing to do with the issue. Are you sure this log is for your process and not some system process?

okhttp reference

picasso reference (as an example of how a library may be at fault)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM