有没有办法安全删除指向 MyObject 的非唯一指针的 QList?

发布于 2024-10-30 22:52:01 字数 868 浏览 1 评论 0 原文

我知道这种问题已经被问到死了,但我想知道是否有办法做我在问题中所说的而不使用Boost库指针等。基本上我有以下一段删除二维 QList ( QList< QList > ) 中指针指向的对象的清理代码,

#include <QList>
QList< QList<MyObject*> > m_Data;
...
void CleanupMyObjectList
{
int numFrames = m_Data.size();
for(int i=0; i < numFrames; i++)
{
    int numObjects = m_Data.at(i).size();

    for(int j=0; j < numObjects; j++)
    {
        MyObject* removedObject = m_Data[i].at(j);
        if(removedObject != NULL)
        {
            delete removedObject;//This assumes that the pointers are UNIQUE in this list!!! If not it will crash!!!
            removedObject = NULL;
        }
    }
    m_Data[i].clear();

}

m_Data.clear();
}

当我尝试清理由非唯一或共享填充的列表时,它确实崩溃了(这是正确的术语吗?)指针,例如当 m_Data[0][1] 等于 m_Data[1][1] 时。

我知道它为什么崩溃,所以请保存对此的解释,但我想知道是否有办法安全地删除这些对象,并尽可能少地修改代码。

I know this kind of question has been asked to death but I would like to know if there is anyway to do what I stated in the question without using Boost library pointers etc. Basically I have the following piece of cleanup code that deletes objects pointed to by the pointers in the double-dimension QList ( QList< QList > )

#include <QList>
QList< QList<MyObject*> > m_Data;
...
void CleanupMyObjectList
{
int numFrames = m_Data.size();
for(int i=0; i < numFrames; i++)
{
    int numObjects = m_Data.at(i).size();

    for(int j=0; j < numObjects; j++)
    {
        MyObject* removedObject = m_Data[i].at(j);
        if(removedObject != NULL)
        {
            delete removedObject;//This assumes that the pointers are UNIQUE in this list!!! If not it will crash!!!
            removedObject = NULL;
        }
    }
    m_Data[i].clear();

}

m_Data.clear();
}

and it indeed crashes when I try to clean up the list populated by non-unique or shared (is this the correct term?) pointers, for example when m_Data[0][1] is equal to m_Data[1][1].

I know why it crashes so please save the explanation for that but rather I want to know if there is anyway to safely delete these objects with as little modification of the code as possible.

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

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

发布评论

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

评论(2

随风而去 2024-11-06 22:52:02

最简单的方法是创建一个临时 std::set。迭代 m_Data,而不是立即删除指针,而是将它们添加到集合中。接下来,删除集合中的所有指针。 std::set 中没有重复项。

您可以安全地删除 NULL 指针。无需检查

The easiest way is to create a temporary std::set<MyObject*>. Iterate over m_Data, and instead of deleting the pointers straight away, add them to the set. Next, delete all pointers in the set. There are no duplicates in a std::set.

You can safely delete a NULL pointer. No need to check that

坚持沉默 2024-11-06 22:52:02

因为你不想使用 boost 共享指针,
您可以使用 QListQList< QList> > m_Data; 而不是 QList< QList; > m_Data;

Because u don't want to use boost shared pointers ,
you can use QList< QList<QSharedPointer<MyObject>> > m_Data; instead of QList< QList<MyObject*> > m_Data;

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