异常安全:

Exception safety:

本文关键字:安全 异常      更新时间:2023-10-16

我想征求一些关于异常安全的建议。特别是我一直在引用你(真的)写异常安全代码吗?如果我有一个指向类型为Node的对象的指针容器,并且我要用一个新的对象集合清除和重新初始化该对象容器_nodes,这段代码是否异常安全?

std::vector<Node*> nodes;
for (int i = 0; i < 10; i++)
{
    try
    {
        // New can throw an exception. We want to make sure that if an exception is thrown any allocated memory is deleted.
        std::unique_ptr<Node> node(new Node());
        Node* n = node.get();
        nodes.push_back(n);
        node.release();
    }
    catch (std::exception& exception)
    {
        // If an exception is thrown, rollback new allocations and rethrow the exception.
        for (std::vector<Node*>::iterator it = nodes.begin(); it < nodes.end(); it++)
        {
            delete *it;
        }
        nodes.clear();
        throw exception;
    }
}
_nodes.swap(nodes);
// Delete the unused (previous) objects from the swapped container.
for (std::vector<Node*>::iterator it = nodes.begin(); it < nodes.end(); it++)
{
    delete *it;
}

我也一直在阅读RAII,但我不知道这将如何工作,我需要多态性(http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping)。

这比它需要的要复杂得多。我将这样开始:

std::vector<std::unique_ptr<Node>> nodes(10);    
for (auto& p : nodes)
    p.reset(new Node());

如果构造向量或分配Node抛出,则所有内容将自动清理。

现在,如果你是明智的,用std::vector<std::unique_ptr<Node>>替换_nodes,那么剩下的功能是简单的:

_nodes.swap(nodes);

否则就不那么简单了:

std::vector<Node*> nodes2;
nodes2.reserve(nodes.size());
for (auto p : nodes)
    nodes2.push_back(p.release());
_nodes.swap(nodes2);
for (auto p : nodes2)
    delete p;

假设Node析构函数不能抛出,这里唯一可以抛出的步骤是reserve调用,所以如果抛出nodes就会被清理,因为它持有unique_ptr对象。之后,您可以安全地将所有权转移到nodes2,然后交换,然后清理。

我也一直在阅读RAII,但我不知道这将如何工作,我需要多态性

我上面的代码依赖于RAII,多态性是无关紧要的。上面的代码中没有不属于RAII类型的原始指针(_nodes除外,您应该将其更改为std::vector<std::unique_ptr<Node>>),因此如果抛出异常,则所有内容都将被清理,无需捕获异常并进行手动清理。