1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-16 04:12:04 +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:
Florian Schmaus 2013-05-17 22:04:04 +00:00 committed by flow
parent 10a317f570
commit a934624787

View file

@ -2590,11 +2590,13 @@ public class MultiUserChat {
*/ */
public static InvitationsMonitor getInvitationsMonitor(Connection conn) { public static InvitationsMonitor getInvitationsMonitor(Connection conn) {
synchronized (monitors) { 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 // We need to use a WeakReference because the monitor references the
// connection and this could prevent the GC from collecting the monitor // connection and this could prevent the GC from collecting the monitor
// when no other object references 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 the InvitationsMonitor that monitors the connection
return monitors.get(conn).get(); return monitors.get(conn).get();