moagrius / MapView

(Deprecated, prefer https://github.com/moagrius/TileView) Android widget roughly described as a hybrid between com.google.android.maps.MapView and iOS's CATiledLayer
http://moagrius.github.com/MapView/documentation
69 stars 35 forks source link

TileManager leaks on orientationchange #32

Closed frankowskid closed 11 years ago

frankowskid commented 11 years ago

TileManager will leak on orientation change. The reason for memory leak is inner class of Handler instance. This should be static.

Example solution would be.

static private Handler handler = new Handler() { @Override public void handleMessage(final Message message) { switch ( message.what ) { case RENDER_FLAG : TileManager manager = (TileManager)message.obj; manager.renderTiles(); break; }}};

public void requestRender() {
    // if we're requesting it, we must really want one
    renderIsCancelled = false;
    renderIsSuppressed = false;
    // if there's no data about the current zoom level, don't bother
    if ( zoomLevelToRender == null ) {
        return;
    }
    // throttle requests
    if ( handler.hasMessages( RENDER_FLAG ) ) {
        handler.removeMessages( RENDER_FLAG );
    }
    // give it enough buffer that (generally) successive calls will be captured
    Message message = handler.obtainMessage(RENDER_FLAG, this);
    handler.sendMessageDelayed(message, RENDER_BUFFER);
}
moagrius commented 11 years ago

Thanks for the post. Orientation change has a bunch of problems #21, which also extends to anything that needs to deref or cleanup (onPause, onDestroy) - I'm trying to get an update committed this week to start getting and handle on this. I'll keep your patch handy.

frankowskid commented 11 years ago

another huge leak is made by inner class of RenderTask

moagrius commented 11 years ago

Thanks for the feedback. I'm moving all the anonymous nested inner class Handlers away from that setup. I know lint is reporting that warning. I should have that done fairly soon.

moagrius commented 11 years ago

I've made an update with respect to the lint reports of potential leaks.