1

終了に使用するときに問題があります。私のpthread_exit()コードは次のようなものです:

{
    ...
    pthread_attr_t attr;
    iRetValue = pthread_attr_init(&attr);
    iRetValue = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    size_t nStackSize = 4 * 1024 * 1024;
    iRetValue = pthread_attr_setstacksize(&attr, nStackSize);
    while(condition)
    {
        ...
        int32 iSockId = cServSocket.accept();
        if(iSockId < 0)
        {
             continue;
        }  
        pthread_t tid;
        int32* pSockId = new int(iSockId);
        iRetValue = pthread_create(&tid, &attr, run_thread, (void*)pSockId);
        ...
    }
    ...
    pthread_attr_destroy(&attr);
}

void* run_thread(void* p)
{
    int32 iSockId = *(int32*)p;
    if(p != NULL){delete p}
    int32 iRetValue = g_dealMgr.deal_thread(iSockId);

    return (void*)iRetValue;
}
int32 CDealMgr::deal_thread(int32 iSocket)
{
    ...   // many temporarydata create,expect will release autoly
    pthread_exit((void*)1);
    return 0;
} 

実際、それはメモリリークを引き起こし、私がに移動 pthread_exit((void*)1);するとrun_thread、このように

void* run_thread(void* p)
{
    int32 iSockId = *(int32*)p;
    if(p != NULL){delete p}
    int32 iRetValue = g_dealMgr.deal_thread(iSockId);
    pthread_exit((void*)1);
    return (void*)iRetValue;
}
int32 CDealMgr::deal_thread(int32 iSocket)
{
    ...   // many temporary data create,expect will release autoly
    return 0;
}

メモリリークがなくなります。さて、私の質問は、呼び出さpthread_exit()れた関数で使用run_thread()するとメモリリークが発生する理由です。誰かが私を助けてくれることを願っています。どうもありがとうございました。

4

1 に答える 1