VirtualBox

Changeset 91999 in vbox for trunk/src


Ignore:
Timestamp:
Oct 22, 2021 11:43:28 AM (3 years ago)
Author:
vboxsync
Message:

Audio/Validation Kit: Fixed memory leaks found by ASAN, added + renamed some functions to streamline client destruction. bugref:10008

Location:
trunk/src/VBox/Devices/Audio
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Audio/AudioTestService.cpp

    r91996 r91999  
    819819}
    820820
     821/**
     822 * Disconnects a client.
     823 *
     824 * @returns VBox status code.
     825 * @param   pThis               The ATS instance.
     826 * @param   pInst               The ATS client to disconnect.
     827 */
    821828static int atsClientDisconnect(PATSSERVER pThis, PATSCLIENTINST pInst)
    822829{
     
    824831
    825832    pInst->enmState = ATSCLIENTSTATE_DESTROYING;
    826     pThis->pTransport->pfnNotifyBye(pThis->pTransportInst, pInst->pTransportClient);
     833
     834    if (   pThis->pTransportInst
     835        && pInst->pTransportClient)
     836    {
     837        if (pThis->pTransport->pfnNotifyBye)
     838            pThis->pTransport->pfnNotifyBye(pThis->pTransportInst, pInst->pTransportClient);
     839
     840        pThis->pTransport->pfnDisconnect(pThis->pTransportInst, pInst->pTransportClient);
     841        /* Pointer is now invalid due to the call above. */
     842        pInst->pTransportClient = NULL;
     843    }
    827844
    828845    return VINF_SUCCESS;
     
    837854static void atsClientFree(PATSCLIENTINST pInst)
    838855{
     856    if (!pInst)
     857        return;
     858
     859    /* Make sure that there is no transport client associated with it anymore. */
     860    AssertReturnVoid(pInst->enmState = ATSCLIENTSTATE_DESTROYING);
     861    AssertReturnVoid(pInst->pTransportClient == NULL);
     862
    839863    if (pInst->pszHostname)
    840864        RTStrFree(pInst->pszHostname);
    841865    RTMemFree(pInst);
     866    pInst = NULL;
    842867}
    843868
     
    959984    }
    960985
     986    if (papInsts)
     987    {
     988        for (size_t i = 0; i < cClientsMax; i++)
     989            RTMemFree(papInsts[i]);
     990        RTMemFree(papInsts);
     991    }
     992
    961993    return rc;
    962994}
     
    9921024
    9931025        /*
    994          * New connection, create new client structure and spin of
     1026         * New connection, create new client structure and spin off
    9951027         * the request handling thread.
    9961028         */
     
    12361268    {
    12371269        RTListNodeRemove(&pIt->NdLst);
    1238 
    1239         RTMemFree(pIt);
    1240         pIt = NULL;
     1270        atsClientDisconnect(pThis, pIt);
     1271        atsClientFree(pIt);
    12411272    }
    12421273
  • trunk/src/VBox/Devices/Audio/AudioTestServiceClient.cpp

    r91996 r91999  
    321321    if (pClient->pTransport)
    322322    {
    323         pClient->pTransport->pfnStop(pClient->pTransportInst);
    324323        pClient->pTransport->pfnDestroy(pClient->pTransportInst);
    325         pClient->pTransport = NULL;
     324        pClient->pTransportInst = NULL; /* Invalidate pointer. */
    326325    }
    327326}
     
    579578        if (pClient->pTransport->pfnNotifyBye)
    580579            pClient->pTransport->pfnNotifyBye(pClient->pTransportInst, pClient->pTransportClient);
     580
     581        pClient->pTransport->pfnDisconnect(pClient->pTransportInst, pClient->pTransportClient);
     582        pClient->pTransportClient = NULL;
     583
     584        pClient->pTransport->pfnStop(pClient->pTransportInst);
    581585    }
    582586
  • trunk/src/VBox/Devices/Audio/AudioTestServiceInternal.h

    r91996 r91999  
    131131     *                              or \c false if not (regular client). Optional and can be NULL.
    132132     * @param   ppClientNew         Where to return the allocated client on success.
     133     *                              Must be destroyed with pfnDisconnect() when done.
    133134     */
    134135    DECLR3CALLBACKMEMBER(int, pfnWaitForConnect, (PATSTRANSPORTINST pThis, RTMSINTERVAL msTimeout, bool *pfFromServer, PPATSTRANSPORTCLIENT ppClientNew));
     136
     137    /**
     138     * Disconnects a client and frees up its resources.
     139     *
     140     * @param   pThis               The transport instance.
     141     * @param   pClient             Client to disconnect.
     142     *                              The pointer will be invalid after calling.
     143     */
     144    DECLR3CALLBACKMEMBER(void, pfnDisconnect, (PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient));
    135145
    136146    /**
  • trunk/src/VBox/Devices/Audio/AudioTestServiceTcp.cpp

    r91996 r91999  
    124124/**
    125125 * Disconnects the current client and frees all stashed data.
     126 *
     127 * @param   pThis           Transport instance.
     128 * @param   pClient         Client to disconnect.
    126129 */
    127130static void atsTcpDisconnectClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
    128131{
    129132    RT_NOREF(pThis);
     133
     134    LogRelFlowFunc(("pClient=%RTsock\n", pClient->hTcpClient));
    130135
    131136    if (pClient->hTcpClient != NIL_RTSOCKET)
     
    147152        pClient->pbTcpStashed = NULL;
    148153    }
     154}
     155
     156/**
     157 * Free's a client.
     158 *
     159 * @param   pThis           Transport instance.
     160 * @param   pClient         Client to free.
     161 *                          The pointer will be invalid after calling.
     162 */
     163static void atsTcpFreeClient(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
     164{
     165    if (!pClient)
     166        return;
     167
     168    /* Make sure to disconnect first. */
     169    atsTcpDisconnectClient(pThis, pClient);
     170
     171    RTMemFree(pClient);
     172    pClient = NULL;
    149173}
    150174
     
    432456        if (pClient)
    433457        {
    434             RTTcpServerDisconnectClient2(pClient->hTcpClient);
    435 
    436             RTMemFree(pClient);
     458            atsTcpFreeClient(pThis, pClient);
    437459            pClient = NULL;
    438460        }
     
    495517    while (rc == VERR_INTERRUPTED);
    496518
     519    LogRelFlowFunc(("pClient=%RTsock, rc=%Rrc\n", pClient->hTcpClient, rc));
     520
    497521    /*
    498522     * Disconnect the client.
    499523     */
    500     LogRelFlowFunc(("atsTcpDisconnectClient(%RTsock) (RTTcpWrite rc=%Rrc)\n", pClient->hTcpClient, rc));
    501524    atsTcpDisconnectClient(pThis, pClient);
    502525}
     
    701724
    702725/**
     726 * @interface_method_impl{ATSTRANSPORT,pfnDisconnect}
     727 */
     728static DECLCALLBACK(void) atsTcpDisconnect(PATSTRANSPORTINST pThis, PATSTRANSPORTCLIENT pClient)
     729{
     730    atsTcpFreeClient(pThis, pClient);
     731}
     732
     733/**
    703734 * @interface_method_impl{ATSTRANSPORT,pfnPollIn}
    704735 */
     
    769800static DECLCALLBACK(int) atsTcpDestroy(PATSTRANSPORTINST pThis)
    770801{
     802    /* Stop things first. */
     803    atsTcpStop(pThis);
     804
    771805    /* Finally, clean up the critical section. */
    772806    if (RTCritSectIsInitialized(&pThis->CritSect))
     
    774808
    775809    RTMemFree(pThis);
     810    pThis = NULL;
    776811
    777812    return VINF_SUCCESS;
     
    909944    /* .pfnStop           = */ atsTcpStop,
    910945    /* .pfnWaitForConnect = */ atsTcpWaitForConnect,
     946    /* .pfnDisconnect     = */ atsTcpDisconnect,
    911947    /* .pfnPollIn         = */ atsTcpPollIn,
    912948    /* .pfnPollSetAdd     = */ atsTcpPollSetAdd,
  • trunk/src/VBox/Devices/Audio/testcase/tstAudioTestService.cpp

    r91994 r91999  
    154154    RTTEST_CHECK_RC_OK(hTest, rc);
    155155
     156    AudioTestSvcClientDestroy(&Client);
     157
    156158    rc = AudioTestSvcStop(&Srv);
    157159    RTTEST_CHECK_RC_OK(hTest, rc);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette