用于 POCO 和 Fixtures 的 C++ 智能指针
C++ Smart pointers for POCO and Fixtures
我目前正在努力思考如何正确使用 C++ 智能指针。我一直读到我应该在大多数时候使用 make_unique(),但我总是遇到段错误,我不确定如何正确使用它们。
我正在尝试实例化一些生成的协议缓冲区 classes 用于测试目的。这是我正在尝试做的事情:
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());
}
每当我在我的 Catch2 测试中实例化我的 Fixtures class 时,当我尝试获取 DescendingMessage
的实例时,我会遇到分段错误。我知道这与内存被释放两次有关,但我不知道如何正确解决这个问题。我尝试使用 shared_ptr 并且它做了同样的事情。我在这里错过了什么?我真的很想使用智能指针,但我现在一无所获:/
您需要传递智能指针的所有权。
您目前正在使用 unique_ptr::get()
,其中只有 returns 原始指针。
传递所有权时使用 unique_ptr::release()
指针并释放所有权。
在您的代码中:
// 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());
我目前正在努力思考如何正确使用 C++ 智能指针。我一直读到我应该在大多数时候使用 make_unique(),但我总是遇到段错误,我不确定如何正确使用它们。
我正在尝试实例化一些生成的协议缓冲区 classes 用于测试目的。这是我正在尝试做的事情:
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());
}
每当我在我的 Catch2 测试中实例化我的 Fixtures class 时,当我尝试获取 DescendingMessage
的实例时,我会遇到分段错误。我知道这与内存被释放两次有关,但我不知道如何正确解决这个问题。我尝试使用 shared_ptr 并且它做了同样的事情。我在这里错过了什么?我真的很想使用智能指针,但我现在一无所获:/
您需要传递智能指针的所有权。
您目前正在使用 unique_ptr::get()
,其中只有 returns 原始指针。
传递所有权时使用 unique_ptr::release()
指针并释放所有权。
在您的代码中:
// 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());