Project

General

Profile

« Previous | Next » 

Revision 19e77994

Added by Leszek Koltunski almost 4 years ago

Fix for a crash when exiting an app:

Activity.onPause() -> MessageSender.stopSending sets mThis to null, then a last message gets sent and a crash in newMessage().

Fix this by

1) making the Message inner class static (so that we can create a new one without a reference to mThis)
2) move the locking from mThis (which is not final) to a specialized final Object mLock.

View differences:

src/main/java/org/distorted/library/message/EffectMessageSender.java
31 31
 */
32 32
public final class EffectMessageSender extends Thread
33 33
  {
34
  private class Message
34
  private static class Message
35 35
    {
36 36
    EffectListener mListener;
37 37
    long mEffectID;
......
42 42
      mEffectID = effectID;
43 43
      }
44 44
    }
45
  
46
  private static Vector<Message> mList =null;
47
  private static EffectMessageSender mThis=null;
45

  
46
  private static final Object mLock        = new Object();
47
  private static Vector<Message> mList     = null;
48
  private static EffectMessageSender mThis = null;
48 49
  private static volatile boolean mNotify  = false;
49 50
  private static volatile boolean mRunning = false;
50 51

  
......
69 70
      }
70 71
    else  
71 72
      {  
72
      synchronized(mThis)
73
      synchronized(mLock)
73 74
        {
74
        mThis.notify();
75
        mLock.notify();
75 76
        }
76 77
      }
77 78
    }
......
84 85

  
85 86
    if( mThis!=null )
86 87
      {
87
      synchronized(mThis)
88
      synchronized(mLock)
88 89
        {
89
        mThis.notify();
90
        mLock.notify();
90 91
        }
91 92
      }
92 93
    }
......
105 106
        tmp.mListener.effectFinished(tmp.mEffectID);
106 107
        }
107 108

  
108
      synchronized(mThis)
109
      synchronized(mLock)
109 110
        {
110 111
        if (!mNotify)
111 112
          {
112
          try  { mThis.wait(); }
113
          catch(InterruptedException ex) { }
113
          try  { mLock.wait(); }
114
          catch(InterruptedException ignored) { }
114 115
          }
115 116
        mNotify = false;
116 117
        }
......
133 134
      for(int i=0; i<numListeners; i++)
134 135
        {
135 136
        EffectListener listener = effect.removeFirstListener();
136
        Message msg = mThis.new Message(listener,id);
137
        Message msg = new Message(listener,id);
137 138
        mList.add(msg);
138 139
        }
139 140

  
140
      synchronized(mThis)
141
      synchronized(mLock)
141 142
        {
142 143
        mNotify = true;
143
        mThis.notify();
144
        mLock.notify();
144 145
        }
145 146
      }
146 147
    }

Also available in: Unified diff