简体   繁体   English

从 function 返回共享指针 vs 在 Lambda 中捕获共享指针

[英]Return a shared pointer from a function vs Capturing a shared pointer in a Lambda

I am constructing a shared pointer in a function_1 and giving it as a capture to a lambda.我正在function_1中构造一个共享指针,并将其作为对 lambda 的捕获。

I think this is an issue, could you please confirm if this safe or I am right and I shoudn't be doing this?我认为这是一个问题,您能否确认这是否安全,或者我是对的,我不应该这样做?

#include <memory>
#include <future>
#include <iostream>
#include <vector>

using Op = std::function<double(std::size_t)>;
struct A
{
    std::shared_ptr<const std::vector<double>> function_1() const
    {
        const std::vector<double> v = {1.0, 2.0};
        auto pVec = std::make_shared<const std::vector<double>>(v);
        return pVec;
    };
};

struct B
{
    B() { m_a = A(); };
    
    void populate_op(Op& op) const
    {
        std::shared_ptr<const std::vector<double>> pVec = m_a.function_1();
        op = [pVec](const std::size_t index) -> double
        {
            return pVec->at(index);
        };
    }
    
    void run()
    {
        m_futures.resize(2u);
        Op op0;
        populate_op(op0);
        m_futures[0u] = std::async(std::launch::async, op0, 0u);
        
        Op op1;
        populate_op(op1);
        m_futures[1u] = std::async(std::launch::async, op1, 1u);
        
        const double res0 = m_futures[0u].get();
        const double res1 = m_futures[1u].get();
        
        std::cout << res0 + res1 << std::endl;
        //clean futures
    }
    
private:
    A m_a;
    mutable std::vector<std::future<double>> m_futures;
};

int main() {
    B b;
    b.run();
    return 0;
}

Wont the v be destructed as it's a local variable and I will end up with a moved shared pointer pointing to a destructed object and hence capturing the latter in the lambda? v是否会被破坏,因为它是一个局部变量,我最终会得到一个指向已破坏的 object 的移动共享指针,从而在 lambda 中捕获后者?

shared_ptr is a smart pointer to dynamically-allocated storage holding the value that is shared. shared_ptr是指向动态分配存储的智能指针,存储共享的值。 make_shared allocates that storage and constructs the value by passing the passed arguments to the constructor of the type. make_shared分配该存储并通过将传递的 arguments 传递给该类型的构造函数来构造该值。 In this case, since you are constructing a vector by passing a vector , it calls vector 's copy constructor.在这种情况下,由于您是通过传递一个vector vector所以它会调用vector的复制构造函数。 The vector in the shared storage and the vector that you passed are completely separate objects;共享存储中的vector和你传递的vector是完全独立的对象; it's just that one was constructed by copying the other.只是一个是通过复制另一个来构建的。

So yes, v will be destructed when v 's scope ends.所以是的,当v的 scope 结束时v将被破坏。 This has nothing to do with the separate vector in the shared storage that was dynamically allocated.这与动态分配的共享存储中的单独vector无关。 That vector will be destructed (and the memory freed) when the reference counting determines that there are no more shared_ptr 's pointing to that shared storage.当引用计数确定不再有shared_ptr指向该共享存储时,该vector将被破坏(并释放 memory)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM