longack
longack

Reputation: 115

call_once throw exception with error code -1

the result of the code below is Unknown error -1.

widget.h:

#ifndef WIDGET_H_
#define WIDGET_H_

#include <memory>

class widget
{
public:
    static widget& get_instance();

private:
    static std::unique_ptr<widget> instance;
};

#endif

widget.cpp:

#include <mutex>
#include <thread>
#include "class.h"

std::unique_ptr<widget> widget::instance;
std::once_flag is_create;

widget& widget::get_instance()
{
    std::call_once(is_create, [=]{ instance = std::make_unique<widget>(); });
    return *instance;
}

main.cpp:

#include "widget.h"
#include <iostream>
#include <exception>

int main(int argc, char* argv[]) 
{
    try
    {
        widget::get_instance();
    }
    catch (std::system_error e)
    {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

this says that

std::system_error if any condition prevents calls to call_once from executing as specified

Is this means make_unique failed ?

I trun to gdb and it seems that something wrong in if (__gthread_active_p ()) line and __gthread_active_ptr == 0. I cannot understand that

why it will throw this exception.

Upvotes: 3

Views: 170

Answers (0)

Related Questions