0

I have following simple singleton, the std::call_once is just to make sure that things are broken, I intend to remove it as soon as I have this working.

std::once_flag flag1;

ProfileModel* ProfileModel::instance()
{
    std::call_once(flag1, [](){ qDebug() << "Simple example: called once\n"; });
    static ProfileModel self;
    qDebug() << &self ;
    return &self;
}

Looks simple, and from what I know from C++ this should work. but I'm having two pointers returned by this call, and call once also prints twice.

Simple example: called once
Constructing Konsole::ProfileModel(0x7ff7378a31f0)
Simple example: called once
Constructing Konsole::ProfileModel(0x5592aeb87160)

The only thing I did that I'm not sure it matters, is that I have this inside of a static library, and I only call this twice, once inside of this library, once outside - in the main application.

Anyone can share a bit of light?

Tomaz Canabrava
  • 2,320
  • 15
  • 20

1 Answers1

1

It's a duplicate of Static variable is initialized twice , with the difference that here it's not a static variable, but a method that has a static variable.

Tomaz Canabrava
  • 2,320
  • 15
  • 20