jeudi 29 juin 2017

Previous Object becomes NULL after TlsSetValue

In my C++ dll I am using Thread local storage to store and retrieve thread specific information.

The thread specific info gets packed in a object pInfo. I am allocating once in DLL attach and then using storing the pInfo for each thread using TlsSetValue.

PInfo *pInfo = TlsGetValue(tlsIndex);
if(pInfo == NULL)
{
   pInfo = new PInfo();
   errCheck = TlsSetValue(tlsIndex, pInfo);
}

After some function callbacks and filling the pInfo. In a particular point i push this pInfo (a pointer) to a static vector (which is defined as below).

vector<PInfo*>* swapBucket;

Using swapBucket->push_back(pInfo); i push this object . After pushing this , i need to refresh the existing TLS index storage. So i am assigning NULL to existing TLS index.

errCheck = TlsSetValue(tlsIndex, NULL);

After setting this, i verify whether the lastly inserted pInfo in the swapBucket is valid. But surprisingly it is NULL.

       if(swapBucket->size() > 0)
        {
            if(swapBucket->back() == NULL)
            {
                fileLogObj->WriteLog("[WARNING]","Last Element is NULL",2);
            }
        }

This does not happens all the time. Only some times it happens. (May be once in 5000 times - Sometime it never happens). Most of the time whatever object i push into the vector remains same(even after refreshing with TlsSetValue). Why the pushed object to vector becomes NULL.? (I am not deleting the object too - After some processing with those vector objects i delete those pInfos)

Aucun commentaire:

Enregistrer un commentaire