mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-27 00:32:07 +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/trunk@13648 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
638d34fd06
commit
37f6bf12da
1 changed files with 4 additions and 2 deletions
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue