关于向量中重复的unique_ptr项的所有权错误

问题描述 投票:0回答:2

考虑以下代码,我该如何解决我所面临的所有权问题?

我需要处理这种情况,而不是制作Item的副本,因为我做不到。

ItemItemContainer是API的一部分,用户不应该关注重复,这意味着如果用户为同一个指针调用两次,我需要静默添加一个指针。

我以为我的删除器会解决它,但事实并非如此。

我不能使用unordered_set例如,我应该像现在一样管理这种情况。

ItemContainer.h:

typedef std::unique_ptr<Item, std::function<void(Item *)>> ItemDeleter;
std::vector<ItemDeleter> items_;

ItemContainer.cpp:

void ItemContainer::addItem(Item *item)
{
  ItemDeleter uniqPtrItem(item, [](Item *p) {
    if (p != nullptr) { 
      delete p; //it's crashing here, obviously
      p = nullptr;
    }
  });
  items_.push_back(std::move(uniqPtrTask));
}

main.cpp中

int main() {
  Item *item = new Item();
  ItemContainer itemContainer;
  itemContainer.addItem(item);
  itemContainer.addItem(item);
}
c++ c++11 smart-pointers stdvector move-semantics
2个回答
1
投票

如果你不能使用std::shared_ptr(这是显而易见且最自然的解决方案)那么你可以引用你的Items计数。这是一些简单的演示代码,展示了如何做到这一点:

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

class RefCount
{
public:
    void Retain ()
    {
        ++refcount;
    }

    void Release ()
    {
        if (--refcount == 0)
            delete this;
    }

protected:
    virtual ~RefCount () {}

private:    
    int refcount = 1;
};

class Item : public RefCount
{
public:
    Item () { std::cout << "Item constructor\n"; }
private:
    ~Item () { std::cout << "Item destructor\n"; }
};

typedef std::unique_ptr <Item, void (*) (Item *)> ItemDeleter;

void addItem (std::vector <ItemDeleter> &items, Item *item)
{
    item->Retain ();
    ItemDeleter uniqPtrItem (item, [] (Item *p) { p->Release (); });
    items.push_back (std::move (uniqPtrItem));
}

int main()
{
    std::vector <ItemDeleter> items;
    Item *item = new Item;
    addItem (items, item);
    addItem (items, item);
    item->Release ();
}

输出:

Item constructor
Item destructor

Live demo


0
投票

考虑到你的情况所带来的限制,我就是这样解决的。

如果他们将原始指针传入容器,使用shared_ptr将无法工作,因为您仍然需要检查指针是否包含在容器中的任何其他位置。这同样适用于unique_ptr - 您必须检查它是否出现在容器中的任何其他位置。

没有办法绕过它:你必须检查容器是否已经有物品。话虽这么说,我们可以使用unordered_map有效地做到这一点。地图将跟踪每个项目在容器中出现的次数,当项目不再出现在容器中时,可以安全地删除它。

template<class Item>
class ItemContainer {
    std::unordered_map<Item*, int> item_counts;
    std::vector<Item*> items; 
   public:
    ItemContainer() = default;
    ItemContainer(ItemContainer&&) = default;
    ItemContainer(ItemContainer const&) = default; 

    void addItem(Item* item) {
        item_counts[item] += 1;
        items.push_back(item); 
    }
    void removeTopItem() {
        // Get the top item and remove it from the vector
        auto item = items.back(); 
        items.pop_back(); 
        // Find the number of times the item appears in the vector
        auto iter = item_counts.find(item);
        auto& count = iter->second;

        if(count == 1) {
            // If it appeared only once, erase it from the map and delete the item
            item_counts.erase(iter); 
            delete item; 
        } else {
            // Otherwise, just update the count
            count -= 1;
        }
    }
    ~ItemContainer() {
        for(auto& count : item_counts) {
            auto item = count.first;
            delete item; 
        }
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.