mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 12:02:05 +01:00
SMACK-438 Avoid NPE when the weak reference is null. Add InvitationsMonitor as strong reference within getInvitationsMonitor and return it within the block so it can't get gc'ed between put() and get().
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_1@13658 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
10a317f570
commit
a934624787
1 changed files with 4 additions and 2 deletions
|
@ -2590,11 +2590,13 @@ public class MultiUserChat {
|
|||
*/
|
||||
public static InvitationsMonitor getInvitationsMonitor(Connection conn) {
|
||||
synchronized (monitors) {
|
||||
if (!monitors.containsKey(conn)) {
|
||||
if (!monitors.containsKey(conn) || monitors.get(conn).get() == null) {
|
||||
// We need to use a WeakReference because the monitor references the
|
||||
// connection and this could prevent the GC from collecting the monitor
|
||||
// when no other object references the monitor
|
||||
monitors.put(conn, new WeakReference<InvitationsMonitor>(new InvitationsMonitor(conn)));
|
||||
InvitationsMonitor ivm = new InvitationsMonitor(conn);
|
||||
monitors.put(conn, new WeakReference<InvitationsMonitor>(ivm));
|
||||
return ivm;
|
||||
}
|
||||
// Return the InvitationsMonitor that monitors the connection
|
||||
return monitors.get(conn).get();
|
||||
|
|
Loading…
Reference in a new issue