jeudi 18 juin 2020

shared_ptr use_count() return 0 after assignment operator (=)

Im new to shared_ptr and can't wrap my around this,

I want to store shared_ptr in custom struct.

struct ThreadSafeQMsg
{
    ThreadSafeQMsg(shared_ptr<ptree> control, shared_ptr<uint32_t> data=0, int data_size=0) {
        data_size = data_size; 
        data = data;
        control = control;
    }
    size_t data_size;
    shared_ptr<ptree> control;
    shared_ptr<uint32_t> data;
};

    ThreadSafeQMsg* tmsg = new ThreadSafeQMsg(control,spData, data_size);
    PRINT_LOG("message with data[" << std::dec << tmsg->data_size << ",ref: " << std::dec << tmsg->data.use_count() 
                                 << "] & control[ref: " << std::dec << tmsg->control.use_count() << "]");

before assignment I see the that the ref count is 1 when checking after assignment I get:

message with data[140737018606128,ref: 0] & control[ref: 0]

what am I missing here?

Aucun commentaire:

Enregistrer un commentaire