lundi 25 septembre 2017

std::once_flag becoming NULL in std::call_once() function causing unhandled exception in C++

WRT below code, when DoSomething() is called the function FirstMod() is supposed to get called only once and it works as expected. But I'm facing a different issue wherein when the function FirstMod() is called several times, suddenly i see an unhandled exception (exception reading a memory location) in the statement.

This exception is because, the value of once_flag object onceMod is NULL.

std::call_once(onceMod,&SomeClass::FirstMod,this, sStr);

i.e. initial few calls to the above statement std::call_once worked fine because, value of once_flag variable onceMod was 1. But but post some calls i see this exception because, the value of Once_flag value "oneMod" becomes NULL.

Not sure why is this std::once onceMod object becoming NULL, who controls this. Do i need to initialize it ?

class SomeClass
{
std::once_flag onceMod;
void FirstMod(std::string sStr)
{

}
void DoSomething(std::string sStr)
{
std::call_once(onceMod,&SomeClass::FirstMod,this, sStr);
...
}
}

Aucun commentaire:

Enregistrer un commentaire