C++;POCO和夹具的智能指针 我目前正试图围绕如何正确使用C++智能指针进行研究。我一直在读我应该在大多数时候使用make_unique(),但我总是会遇到一些问题,我不确定如何正确使用它们

C++;POCO和夹具的智能指针 我目前正试图围绕如何正确使用C++智能指针进行研究。我一直在读我应该在大多数时候使用make_unique(),但我总是会遇到一些问题,我不确定如何正确使用它们,c++,smart-pointers,fixtures,C++,Smart Pointers,Fixtures,我试图实例化一些协议缓冲区生成的类,以进行测试。以下是我正在尝试做的: using namespace std; #include <catch.hpp> class Fixtures { public: std::unique_ptr<DescendingMessage> getDescendingMessage(); std::unique_ptr<VehicleCommand> getVehicleCommand_Door();

我试图实例化一些协议缓冲区生成的类,以进行测试。以下是我正在尝试做的:

using namespace std;
#include <catch.hpp>

class Fixtures {
public:
    std::unique_ptr<DescendingMessage> getDescendingMessage();

    std::unique_ptr<VehicleCommand> getVehicleCommand_Door();

    std::unique_ptr<Door> getDoorCommand_open();



};

unique_ptr<DescendingMessage> Fixtures::getDescendingMessage() {
    auto descendingMessage = make_unique<DescendingMessage>();
    descendingMessage->set_allocated_vehicle_command(getVehicleCommand_Door().get());
    return move(descendingMessage);
}

unique_ptr<VehicleCommand> Fixtures::getVehicleCommand_Door() {
    auto vehicleCommand = make_unique<VehicleCommand>();
    vehicleCommand->set_allocated_door_operation(getDoorCommand_open().get());
    return move(vehicleCommand);
}

unique_ptr<Door> Fixtures::getDoorCommand_open() {
    auto door = make_unique<Door>();
    door->set_mode(Door_Mode_OPEN);
    return move(door);

}


SCENARIO("Get a sample protobuf model from Fixtures and test its content") {
    auto fixtures = make_unique<Fixtures>();
    auto descendingMessage = fixtures->getDescendingMessage();

    REQUIRE(!descendingMessage->has_metadata());
    REQUIRE(!descendingMessage->has_state_request());
    REQUIRE(descendingMessage->has_vehicle_command());

}
使用名称空间std;
#包括
班级固定装置{
公众:
std::unique_ptr getDowningMessage();
std::unique_ptr getVehicleCommand_Door();
std::unique_ptr getDoorCommand_open();
};
唯一的\u ptr Fixtures::GetDenseringMessage(){
自动下降消息=使_唯一();
下降消息->设置分配的车辆命令(getVehicleCommand车门().get());
返回移动(下降消息);
}
唯一的ptr固定装置::getVehicleCommand\u门(){
auto vehicleCommand=使_唯一();
vehicleCommand->set_allocated_door_operation(getDoorCommand_open().get());
返回移动(车辆命令);
}
唯一的\u ptr装置::getDoorCommand\u open(){
自动门=使_唯一();
门->设置门模式(门模式打开);
返回移动(门);
}
场景(“从fixture获取一个样本protobuf模型并测试其内容”){
自动夹具=使_唯一();
自动下降消息=装置->GetDowningMessage();
需要(!DerningMessage->has_metadata());
需要(!DerningMessage->has_state_request());
REQUIRE(下降消息->has_vehicle_command());
}

每当我在Catch2测试中实例化Fixtures类时,当我试图获取
genderingmessage
的实例时,就会出现分段错误。我知道这与两次释放内存有关,但我不知道如何正确解决这个问题。我尝试了shared_ptr,它也做了同样的事情。我错过了什么?我真的很想使用智能指针,但目前我还没有取得任何进展://

您需要传递智能指针的所有权

您当前使用的是只返回原始指针的

传递所有权时,使用返回指针并释放所有权的

在代码中:

// object used to set vehicle command 
//    but object is still owned by unique_ptr
set_allocated_vehicle_command(getVehicleCommand_Door().get());

// object is used to set vehicle command
//    and ownership is released from unique_ptr
set_allocated_vehicle_command(getVehicleCommand_Door().release());

提供的代码不可编译-请使用我的真实代码提供上面的真实更新代码,而不是仍然不符合标准的伪代码-我们无法编译此代码并自行查看。虽然
getVehicleCommand\u Door().get()
向我指出,您在这里传递的是原始指针,这些指针指向的是立即被销毁的对象。我明白了,很抱歉,我不习惯在这里提问:/基本模型是协议缓冲区模型,在这里发布会非常大。在这种情况下(基于函数名),我会找到方法签名,您正在类中存储悬空指针-为什么不让函数本身接受
unique\u ptr
s(并将所述
unique\u ptr
s存储为类成员)?