使QMap中的指针值无效 [英] Invalidate pointer values inside a QMap

查看:615
本文介绍了使QMap中的指针值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有一个奇怪的问题,但这可能是关于QMap的工作方式的古怪之处,我只是不理解.总结问题很难,但我会尽力而为.

I'm having what seems to be a weird issue, but it could be a quirk of how QMap's work and I just don't understand it. It's tough to summarize the problem, but I'll do my best.

我有一个类A和一个QMap<QString, someType*> mySomeTypeMap;.在程序中加载新文件时,我想删除此QMap的所有内容,以便可以用新数据重新填充它.我通过执行以下操作来做到这一点:

I have a class A, with a QMap<QString, someType*> mySomeTypeMap;. When I load a new file in my program, I want to delete all the contents of this QMap so I can repopulate it with new data. I do that by doing the following:

foreach (QString key, mySomeTypeMap.keys())
{
    someType* toDelete = mySomeTypeMap.take(key);

    //Show me the original address of the pointer
    qDebug() << "toDelete: " << toDelete;

    delete toDelete;

    //Set the pointer to 0x0
    toDelete = NULL;
}

qDebug()语句打印出我要删除的值的正确地址,当我将其设置为NULL后在调试器中查看toDelete时,它说的是0x0,这就是我想要的

The qDebug() statement prints out the correct address of the value I want to delete, and when I look at toDelete in the debugger after it gets set to NULL, it says 0x0, which is what I want.

然后,在另一个类B中,我有以下代码...

Then later, in a different class B, I have the following code...

void B::setSomeType(someType* blah)
{
    if (Blah != NULL)
    {
        //Calls a bunch of disconnect()'s
        disconnectAllSignals();

        Blah = blah;

        //Calls a bunch of connect()'s
        connectAllSignals();
    }
}

现在,真正令人困惑的是,我的程序进入disconnectAllSignals();行时崩溃了.原因是它试图在已删除的Blah上调用disconnect(),并且当我将其设置为NULL时应将其设置为0x0.但是,如果实际上将其设置为NULL,则永远不会输入该if块开始.在调试器中,我看到Blah的地址与我在设置toDelete = NULL;之前打印出qDebug() << "toDelete: " << toDelete;时得到的地址完全相同.

Now, what's really confusing is my program crashes when it gets to the disconnectAllSignals(); line. The reason being it's trying to call disconnect() on Blah which was deleted and should have been set to 0x0 when I set it to NULL. However, if it was actually set to NULL it never would have entered that if-block to begin with. In a debugger, I see that the address of Blah is exactly the same as what I get when I print out qDebug() << "toDelete: " << toDelete; right before setting toDelete = NULL;.

TLDR;在删除指针并将相同的指针设置为NULL之后,我不知道我的程序如何获取指针的原始地址.由于在执行过程中未将指针设置为NULL,因此会导致崩溃.

TLDR; I don't know how my program is getting the orignal address of a pointer back after I delete the pointer and set the same pointer to NULL. Since the pointer isn't set to NULL later in the execution it leads to a crash.

推荐答案

即使在这种情况下,映射中的值也按值存储,即使这意味着指针(指针指向的地址)也按值存储. 例如:

Values in the map are stored by value even if in these case it means that the pointer (address which it points) is stored by value. For example:

someType* toDelete1 = mySomeTypeMap.take(key);
someType* toDelete2 = mySomeTypeMap.take(key);
someType* toDelete3 = toDelete1;
toDelete1 = NULL;

toDelete2& toDelete3仍将指向原始对象

toDelete2 & toDelete3 will still point to the original object

代替在删除后使指针为空,您应该将其从映射中删除或将该键的值设置为NULL.

Instead of making the pointer null after delete you should either remove it from the map or set the value for that key to NULL.

这篇关于使QMap中的指针值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆