windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

Passing weak_ptr by reference to lambda

Asked By: Anonymous

I observe an undefined behavior at runtime on my program and I suspect it might have caused by the weak_ptr.

The send function of socket get called but it can’t map the socket, but if I remove Lambdas it can map the socket.

The worker function was synchronous before and had no issues, but it cause undefined behavior now after adding Lambdas into worker function.

Is this really unsafe? if so, what should I use instead?

// Global Socket
std::shared_ptr<s::Channel> gSocket;

void worker(std::weak_ptr<s::Channel> socket)
{
    auto &request = requests.emplace_back();

    request.onMessage([&](std::string data) {
        if (auto channel = socket.lock())
        {
            channel->send(data);
        }
    })

    request.onError([&](std::string data) {
        if (auto channel = socket.lock())
        {
            channel->send(data);
        }
    })
}

int main() {
    broker->onInit([&]() {
        // Shared PTR
        gSocket = createConnection();

        gSocket->onOpen([&, wS = make_weak_ptr(gSocket)]() {
            if (auto c = wS.lock())
            {
                worker(c);
            }
        });
    })

    broker.init();
}

Solution

Answered By: Anonymous

As noted in the comments

std::weak_ptr<s::Channel> socket is a local variable, it is destroyed after the function returns. When the callback is called later, it is referring to a non-existent weak_ptr instance.

You can fix it by capturing socket by value:

void worker(std::weak_ptr<s::Channel> socket)
{
    auto &request = requests.emplace_back();

    request.onMessage([socket, &request](std::string data) {
        if (auto channel = socket.lock())
        {
            channel->send(data);
        }
    })

    request.onError([socket, &request](std::string data) {
        if (auto channel = socket.lock())
        {
            channel->send(data);
        }
    })
}

techinplanet staff

Porno Gratuit Porno Français Adulte XXX Brazzers Porn College Girls Film érotique Hard Porn Inceste Famille Porno Japonais Asiatique Jeunes Filles Porno Latin Brown Femmes Porn Mobile Porn Russe Porn Stars Porno Arabe Turc Porno caché Porno de qualité HD Porno Gratuit Porno Mature de Milf Porno Noir Regarder Porn Relations Lesbiennes Secrétaire de Bureau Porn Sexe en Groupe Sexe Gay Sexe Oral Vidéo Amateur Vidéo Anal

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno