In the core of our project we have following code:
template<typename T>
T& get_factory_instance(bool reset = false)
{
static boost::scoped_ptr<T> factory_instance;
if (reset)
{
factory_instance.reset();
return *(T*)0;
}
if (!factory_instance)
{
factory_instance.reset(new T());
}
return *factory_instance;
}
If reset is true, that it's UB according to standard.
This function with argument true is called only when return value is ignored, so, memory is not accessed. It's mandatory, function cannot be called from services, only from libraries, when we add call of this function to singleton list of functions with signature void(). We need such strange hack for clearing prepared statement connected to the database, if connection lost.
So, basically question is: can it fire, if we don't access this memory? And if yes, how can we possibly fix it without rewriting all code dependent on this function, if there is any possibility?
When T constructor is called it constructs prepared statement and open/use connection to the database, so, creating dummy object is not pretty idea.
We have near 80 calls of this function with argument true in out libraries. It was introduced in 2015. Code that uses this function without argument in most cases looks like:
fields_t& fields = get_factory_instance<fields_t>();
Thanks in advance.