From 37f6bf12da1f5c03712a8a6a8ce0aeba0420b2c4 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 12 May 2013 14:51:41 +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/trunk@13648 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();