diff --git a/src/source/PeerConnection/PeerConnection.c b/src/source/PeerConnection/PeerConnection.c index 31c37d82d8..9c8cde32b3 100644 --- a/src/source/PeerConnection/PeerConnection.c +++ b/src/source/PeerConnection/PeerConnection.c @@ -1605,12 +1605,18 @@ STATUS initKvsWebRtc(VOID) STATUS cleanupWebRtcClientInstance() { STATUS retStatus = STATUS_SUCCESS; + // Stun object cleanup PWebRtcClientContext pWebRtcClientContext = getWebRtcClientInstance(); + + releaseHoldOnInstance(pWebRtcClientContext); + + DLOGD("Releasing webrtc client context instance from cleanupWebRtcClientInstance"); + CHK_WARN(ATOMIC_LOAD_BOOL(&pWebRtcClientContext->isContextInitialized), STATUS_INVALID_OPERATION, "WebRtc context not initialized, nothing to clean up"); - DLOGD("Releasing webrtc client context instance from cleanupWebRtcClientInstance"); - releaseHoldOnInstance(pWebRtcClientContext); + + ATOMIC_STORE_BOOL(&pWebRtcClientContext->isContextInitialized, FALSE); while (ATOMIC_LOAD(&pWebRtcClientContext->contextRefCnt) > 0) { DLOGV("Waiting on all references to be returned...%d", pWebRtcClientContext->contextRefCnt); @@ -1633,8 +1639,6 @@ STATUS cleanupWebRtcClientInstance() pWebRtcClientContext->stunCtxlock = INVALID_MUTEX_VALUE; } - ATOMIC_STORE_BOOL(&pWebRtcClientContext->isContextInitialized, FALSE); - DLOGI("Destroyed WebRtc client context"); CleanUp: