简体   繁体   English

从函数返回对内的 unique_ptr 后出现分段错误

[英]Segmentation fault after returning unique_ptr inside of pair from function

I have a factory that creates class instances from strings.我有一个从字符串创建类实例的工厂。 KeyValueType is an abstract class, which will be passed to Map/Reduce functions. KeyValueType 是一个抽象类,它将传递给 Map/Reduce 函数。

class KeyValueType {
public:
    virtual void parse(const std::string &) = 0;

    virtual std::string to_string() const = 0;
};

Factories in code are getting from the shared library (to be able to config map/reduce functions from a remote computer).代码工厂从共享库中获取(以便能够从远程计算机配置映射/减少功能)。

std::unique_ptr<KeyValueType> KeyValueTypeFactory::create() override {
    return std::make_unique<KeyValueType<T>>();
};
std::unique_ptr<KeyValueType> KeyValueTypeFactory::create(const std::string &str) override {
    std::unique_ptr<KeyValueType> ptr = this->create();
    ptr->parse(str);
    return ptr;
};

So, I have the next code, where I'm creating two objects key/value and returning them, as a pair of unique_ptr所以,我有下一个代码,我在其中创建两个对象键/值并将它们作为一对 unique_ptr 返回

std::pair<std::unique_ptr<KeyValueType>, std::unique_ptr<KeyValueType>> 
get_key_value_from_json(const std::string &data, std::unique_ptr<KeyValueTypeFactory> &key_factory, std::unique_ptr<KeyValueTypeFactory> &value_factory) {
    boost::property_tree::ptree pt{};
    boost::property_tree::json_parser::read_json(dynamic_cast<std::stringstream &>(std::stringstream{} << data), pt);
    return { std::move(key_factory->create(pt.get("key", ""))),
             std::move(value_factory->create(pt.get("value", ""))) };
}
std::pair<std::unique_ptr<KeyValueType>, std::unique_ptr<KeyValueType>> blocking_get_result() {
        ... // Get json and config
        auto[key, value] = get_key_value_from_json(json, cfg->key_out_factory, cfg->value_res_factory);
        std::cout << "The result of Map/Reduce is " << value->to_string() << std::endl;
        return { std::move(key), std::move(value) };
}
int main() {
    auto[key, value] = blocking_get_result();
    std::cout << (value.get() == nullptr) << std::endl;
    std::cout << "The result of Map/Reduce is " << value->to_string() << std::endl;
    return 0;
}

The actual problem is, that in blocking_get_result() function key and value are valid and virtual function to_string() is working correctly, but after returning pair from function to main unique_ptr is not null, but to_string throws Segmentation Fault.实际问题是, blocking_get_result()函数中的key 和value 是有效的,并且虚函数to_string()工作正常,但是从函数返回pair 到main 后unique_ptr不为空,但是to_string抛出Segmentation Fault。 Also, dynamic_cast to a derived class is causing Segfault.此外,对派生类的dynamic_cast会导致段错误。

The actual problem was, that getting config fulfilled by using dlopen and dlsym and wrapping result into shared_ptr.实际的问题是,通过使用dlopendlsym并将结果包装到 shared_ptr 来实现配置。 So shared library was freed in blocking_get_result.所以共享库在blocking_get_result 中被释放。 So in the main pointer in the vtable became invalid.所以在vtable中的main指针就失效了。

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

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