From a9346247873bc664b1ec0732e0f1936e7d03fae1 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 17 May 2013 22:04:04 +0000 Subject: [PATCH] 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 --- source/org/jivesoftware/smackx/muc/MultiUserChat.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/org/jivesoftware/smackx/muc/MultiUserChat.java b/source/org/jivesoftware/smackx/muc/MultiUserChat.java index e0368028d..4800011fd 100644 --- a/source/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/source/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -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(new InvitationsMonitor(conn))); + InvitationsMonitor ivm = new InvitationsMonitor(conn); + monitors.put(conn, new WeakReference(ivm)); + return ivm; } // Return the InvitationsMonitor that monitors the connection return monitors.get(conn).get();