• 12401阅读
  • 4回复

QSharedPointer 的问题 [复制链接]

上一主题 下一主题
离线luhaoting110
 

只看楼主 倒序阅读 楼主  发表于: 2013-05-15
关键词: QSharedPointer
  1. QList< QList< QSharedPointer<QGraphicsItem> > > history;
这是为history加入内容
  1. QList<QSharedPointer<QGraphicsItem > > item_list;
  2.     QSharedPointer<QGraphicsItem > item_cell;
  3.     for(int i = 0 ;i < items(Qt::AscendingOrder).size() ; i++)
  4.     {
  5.         item_cell = QSharedPointer<QGraphicsItem>(items(Qt::AscendingOrder)[i]);
  6.         item_list.append(item_cell);
  7.     }
  8.     history.append(item_list);

  1. for(int i=0 ; i < history.size() ; i++)
  2.             {
  3.                 qDebug()<<"***********这是第"<<i<<"个链表********";
  4.                 qDebug()<<"此项长度为"<<history[i].size()<<"\n";
  5.                 for(int j = 0 ;j < history[i].size();j++)
  6.                 {qDebug()<<history[i][j].data();}
  7.                 qDebug()<<"-------------------------------------";
  8.              }
  9.             //delete_itemList(history.last());
  10.             history.removeLast();
  11.             for(int i=0 ; i < history.size() ; i++)
  12.             {
  13.                 qDebug()<<"***********这是第"<<i<<"个链表********";
  14.                 qDebug()<<"此项长度为"<<history[i].size()<<"\n";
  15.                 for(int j = 0 ;j < history[i].size();j++)
  16.                 {qDebug()<<history[i][j].data();}
  17.                 qDebug()<<"-------------------------------------";
  18.              }

为什么我在没有removeLast();之前可以遍历 ,之后遍历就挂了,难道QSharedPointer帮我delete了?




下面是打印
  1. ***********这是第 0 个链表********
  2. 此项长度为 2
  3. QGraphicsObject (this = 0x126d208 , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags =  ( ItemIsFocusable ) )
  4. QGraphicsItem (this = 0x1359d24 , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags =  ( ) )
  5. -------------------------------------
  6. ***********这是第 1 个链表********
  7. 此项长度为 3
  8. QGraphicsObject (this = 0x126d208 , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags =  ( ItemIsFocusable ) )
  9. QGraphicsItem (this = 0x1359d24 , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags =  ( ) )
  10. QGraphicsItem (this = 0x1359ae4 , parent = 0x0 , pos = QPointF(0, 0) , z = 0 , flags =  ( ) )
  11. -------------------------------------
  12. ***********这是第 0 个链表********
  13. 此项长度为 2
这里就挂了

离线dbzhang800

只看该作者 1楼 发表于: 2013-05-16
引用楼主luhaoting110于2013-05-15 14:01发表的 QSharedPointer 的问题 :
为什么我在没有removeLast();之前可以遍历 ,之后遍历就挂了,难道QSharedPointer帮我delete了?


对。因为你错误地新建了多个指向同一个指针的 QSharedPointer,所以任何一个QSharedPointer的析构都会导致该指针无效!!

建议熟悉一下 shared pointer 等智能指针的用法
离线luhaoting110

只看该作者 2楼 发表于: 2013-05-16
回 1楼(dbzhang800) 的帖子
我有几个问题不懂
首先 Qlist的remove会导致QSharedPointer指针的释放?

其次如果释放了 QSharedPointer的计数器难道不是我这样操作就会累加的嘛?

我的理解是多个QSharedPointer指向同一块内存,QSharedPointer的计数器就加,使用QSharedPointer::clear() 的时候计数器就减少直至为0 的时候就释放这个内存。
离线dbzhang800

只看该作者 3楼 发表于: 2013-05-16
Re:回 1楼(dbzhang800) 的帖子
引用第2楼luhaoting110于2013-05-16 09:44发表的 回 1楼(dbzhang800) 的帖子 :
我有几个问题不懂
首先 Qlist的remove会导致QSharedPointer指针的释放?
其次如果释放了 QSharedPointer的计数器难道不是我这样操作就会累加的嘛?
.......

在用法正确的情况下确实如此。

问题在于你对同一个指针创建了若干个独立的 shared pointer,使得你的列表中保存的每一个shared pointer 引用计数都为1


题外:
对于Qt中这种有父子关系的QObject的指针,使用shared pointer需要更小心一些。或许QPointer或QWeakPointer更合适一些
离线luhaoting110

只看该作者 4楼 发表于: 2013-05-16
懂了 !谢谢大神指点
快速回复
限100 字节
 
上一个 下一个