自定义Unique_ptr删除器,受控删除

发布于 2024-09-29 12:14:02 字数 1910 浏览 2 评论 0原文

我有一个 for 循环,它遍历 XML 文档并查找指定的属性,指向当前节点的指针位于 boost::interprocess::unique_ptr 内,并具有一个自定义删除器来调用对象的 release() 函数。似乎在每次循环迭代中指针都会被删除,但是当发生这种情况时,release() 函数会抛出异常。

有人能建议一个解决方案吗?我考虑过实现一种机制来检查它是否应该被删除,但我不确定如何做到这一点...

代码:

typedef bi::unique_ptr<DOMNodeIterator, release_deleter> iterator_ptr;
typedef bi::unique_ptr<DOMNode, release_deleter> node_ptr;

iterator_ptr itera(document->createNodeIterator(rootelement, DOMNodeFilter::SHOW_ALL, NULL, true));

for(node_ptr current(itera->nextNode()); current != 0; current.reset(itera->nextNode())) // throws after one iteration...
{   

....

对象release()

void DOMElementNSImpl::release()
{
    if (fNode.isOwned() && !fNode.isToBeReleased())
        throw DOMException(DOMException::INVALID_ACCESS_ERR,0, GetDOMNodeMemoryManager);  // throws here if released after each loop iteration

    DOMDocumentImpl* doc = (DOMDocumentImpl*) fParent.fOwnerDocument;
    if (doc) {
        fNode.callUserDataHandlers(DOMUserDataHandler::NODE_DELETED, 0, 0);
        fParent.release();
        doc->release(this, DOMMemoryManager::ELEMENT_NS_OBJECT);
    }
    else {
        // shouldn't reach here
        throw DOMException(DOMException::INVALID_ACCESS_ERR,0, GetDOMNodeMemoryManager);
    }
}

删除器:

struct release_deleter
{
    template <typename T>
    void operator()(T* pPtr) const
    {
        pPtr->release();
    }
};

编辑:

virtual DOMNodeIterator *createNodeIterator(DOMNode* root,
                                            DOMNodeFilter::ShowType whatToShow,
                                            DOMNodeFilter* filter,
                                            bool entityReferenceExpansion) = 0;


virtual DOMNode*           nextNode() = 0;

I have a for loop that iterates through an XML document and finds a specified attribute, the pointer that points to the current node sits inside a boost::interprocess::unique_ptr and has a custom deletor to call the object's release() function. It seems that on every loop iteration the pointer gets deleted, but the release() function throws when that happens.

Could anyone suggest a solution? I thought about implementing a mechanism to check if it should be deleted, but I'm not sure how I'd do that...

Code:

typedef bi::unique_ptr<DOMNodeIterator, release_deleter> iterator_ptr;
typedef bi::unique_ptr<DOMNode, release_deleter> node_ptr;

iterator_ptr itera(document->createNodeIterator(rootelement, DOMNodeFilter::SHOW_ALL, NULL, true));

for(node_ptr current(itera->nextNode()); current != 0; current.reset(itera->nextNode())) // throws after one iteration...
{   

....

objects release()

void DOMElementNSImpl::release()
{
    if (fNode.isOwned() && !fNode.isToBeReleased())
        throw DOMException(DOMException::INVALID_ACCESS_ERR,0, GetDOMNodeMemoryManager);  // throws here if released after each loop iteration

    DOMDocumentImpl* doc = (DOMDocumentImpl*) fParent.fOwnerDocument;
    if (doc) {
        fNode.callUserDataHandlers(DOMUserDataHandler::NODE_DELETED, 0, 0);
        fParent.release();
        doc->release(this, DOMMemoryManager::ELEMENT_NS_OBJECT);
    }
    else {
        // shouldn't reach here
        throw DOMException(DOMException::INVALID_ACCESS_ERR,0, GetDOMNodeMemoryManager);
    }
}

deleter:

struct release_deleter
{
    template <typename T>
    void operator()(T* pPtr) const
    {
        pPtr->release();
    }
};

EDIT:

virtual DOMNodeIterator *createNodeIterator(DOMNode* root,
                                            DOMNodeFilter::ShowType whatToShow,
                                            DOMNodeFilter* filter,
                                            bool entityReferenceExpansion) = 0;


virtual DOMNode*           nextNode() = 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑾夏年华 2024-10-06 12:14:02

我在工作时没有用于编译和调试的 Visual 2010,只能在家里使用。所以我不能确定。

但坦率地说,我真的不喜欢迭代器拥有指针的概念。

您的集合拥有指针,但迭代器不拥有!它不是它的角色。

在C++0x中,有一个对shared_ptr的补充,即std::weak_ptr,您可以从shared_ptr构造它,允许查阅和更改数据,但与所有权无关,除了变得更多或当shared_ptr所拥有的指针被释放时,不易访问。

我会为你的迭代器使用一种 std::weak_ptr 。
但对于 unique_ptr 我猜想它是一个简单的数据指针,充当弱引用的角色。

编辑:

DOMNodeIterator * pIter = document->createNodeIterator(rootelement, DOMNodeFilter::SHOW_ALL, NULL, true);

if(pIter==NULL)
{
    return;
}

while(true) // be careful to infinite loops
{
    DOMNode * pNode = pIter->nextNode();
    if (pNode==NULL)
    {
        break;  
    }

    // ... your visiting the nodes here.
}

I don't have visual 2010 for compiling and debugging at work, on ly at home. so I cannot be sure.

But Frankly I really do not like the concept that your iterator owns your pointer.

Your collection owns the pointer, the iterator doesn't ! its not its role..

In C++0x there are an complement to shared_ptr that is std::weak_ptr that you can construct from shared_ptr , allowing to consult and change the data, but having nothing to do with ownership, except becoming more or less inaccessible when the pointer onwed by the shared_ptr is released.

I would use a sort of std::weak_ptr for your iterators.
But for unique_ptr I would guess it is a simple pointer over your data that takes the role of weak reference.

EDIT:

DOMNodeIterator * pIter = document->createNodeIterator(rootelement, DOMNodeFilter::SHOW_ALL, NULL, true);

if(pIter==NULL)
{
    return;
}

while(true) // be careful to infinite loops
{
    DOMNode * pNode = pIter->nextNode();
    if (pNode==NULL)
    {
        break;  
    }

    // ... your visiting the nodes here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文