3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

Fix memory leak of downloadListener

This commit is contained in:
Matt Corallo 2013-12-11 00:31:40 -05:00 committed by Mike Hearn
parent a0f32ab0d2
commit 2879f7bb9b

View File

@ -82,7 +82,7 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
// The peer that has been selected for the purposes of downloading announced data. // The peer that has been selected for the purposes of downloading announced data.
@GuardedBy("lock") private Peer downloadPeer; @GuardedBy("lock") private Peer downloadPeer;
// Callback for events related to chain download // Callback for events related to chain download
@GuardedBy("lock") private PeerEventListener downloadListener; @Nullable @GuardedBy("lock") private PeerEventListener downloadListener;
// Callbacks for events related to peer connection/disconnection // Callbacks for events related to peer connection/disconnection
private final CopyOnWriteArrayList<ListenerRegistration<PeerEventListener>> peerEventListeners; private final CopyOnWriteArrayList<ListenerRegistration<PeerEventListener>> peerEventListeners;
// Peer discovery sources, will be polled occasionally if there aren't enough inactives. // Peer discovery sources, will be polled occasionally if there aren't enough inactives.
@ -734,12 +734,14 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
public void startBlockChainDownload(PeerEventListener listener) { public void startBlockChainDownload(PeerEventListener listener) {
lock.lock(); lock.lock();
try { try {
if (downloadPeer != null && this.downloadListener != null)
downloadPeer.removeEventListener(this.downloadListener);
this.downloadListener = listener; this.downloadListener = listener;
// TODO: be more nuanced about which peer to download from. We can also try // TODO: be more nuanced about which peer to download from. We can also try
// downloading from multiple peers and handle the case when a new peer comes along // downloading from multiple peers and handle the case when a new peer comes along
// with a longer chain after we thought we were done. // with a longer chain after we thought we were done.
if (!peers.isEmpty()) { if (!peers.isEmpty()) {
startBlockChainDownloadFromPeer(peers.iterator().next()); startBlockChainDownloadFromPeer(peers.iterator().next()); // Will add the new download listener
} }
} finally { } finally {
lock.unlock(); lock.unlock();
@ -877,11 +879,15 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
} }
if (downloadPeer != null) { if (downloadPeer != null) {
log.info("Unsetting download peer: {}", downloadPeer); log.info("Unsetting download peer: {}", downloadPeer);
if (downloadListener != null)
downloadPeer.removeEventListener(downloadListener);
downloadPeer.setDownloadData(false); downloadPeer.setDownloadData(false);
} }
downloadPeer = peer; downloadPeer = peer;
if (downloadPeer != null) { if (downloadPeer != null) {
log.info("Setting download peer: {}", downloadPeer); log.info("Setting download peer: {}", downloadPeer);
if (downloadListener != null)
peer.addEventListener(downloadListener, Threading.SAME_THREAD);
downloadPeer.setDownloadData(true); downloadPeer.setDownloadData(true);
downloadPeer.setDownloadParameters(fastCatchupTimeSecs, bloomFilter != null); downloadPeer.setDownloadParameters(fastCatchupTimeSecs, bloomFilter != null);
} }
@ -990,7 +996,6 @@ public class PeerGroup extends AbstractIdleService implements TransactionBroadca
private void startBlockChainDownloadFromPeer(Peer peer) { private void startBlockChainDownloadFromPeer(Peer peer) {
lock.lock(); lock.lock();
try { try {
peer.addEventListener(downloadListener, Threading.SAME_THREAD);
setDownloadPeer(peer); setDownloadPeer(peer);
// startBlockChainDownload will setDownloadData(true) on itself automatically. // startBlockChainDownload will setDownloadData(true) on itself automatically.
peer.startBlockChainDownload(); peer.startBlockChainDownload();