dimanche 6 octobre 2019

c++ exception on a std::hared_from_this() for a static function

I am trying to make be able to get my configuration anywhere in the process from a static api; because I am limited by the base class configuration I cannot create a singleton

So I have a static pointer outside the class and a static getter to the pointer

my .hpp file

class MyConfigurtion(int argc, char** argv) 
           public Configuration,
           public std::enable_shared_from_this<MyConfiguration> 
{
private:
static void setConfiguration(const std::shared_ptr<MyConfiguration>&);

public:
static const std::shared_ptr<MyConfiguration> getConfiguration();

}

my .cpp file


namespace mynamespace {

static std::shared_ptr<MyConfiguration> ConfigPointer;

//constructor
class MyConfigurtion(int argc, char** argv) 
{
     setConfiguration(shared_from_this());
}

const std::shared_ptr<Configuration> getConfiguration()
{
    return ConfigPointer;
}

}//namespace

I do create a std::make_shared<MyConfiguration>() and then when the setConfiguration() was public things were working

but when I made the setter private on the getConfiguration() I get an exception

unknown location(0): fatal error in "InitializeStoreWriter": std::exception: bad_weak_ptr
unknown location(0): fatal error in "DataStoreOverWriteTest": memory access violation at address: 0x000195f0: no mapping at fault address

So when the setter was made private does it make the access to static variable illegal?

https://en.cppreference.com/w/cpp/memory/bad_weak_ptr

looking for the exception details ..... like the eg in this page I am not using a deleted object to construct an shared object

Aucun commentaire:

Enregistrer un commentaire