summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Wilson <alex.wilson@nokia.com>2012-05-16 11:36:38 +1000
committerQt by Nokia <qt-info@nokia.com>2012-05-16 09:23:55 +0200
commit321e37cb2b04cf075b85399fd97ea77b25f371c1 (patch)
treec45c520413be8a231b075895baf1742368ff29d3
parent18442dafdc42a59984549ae9cca85f5e827009aa (diff)
downloadqtlocation-321e37cb2b04cf075b85399fd97ea77b25f371c1.tar.gz
Keep a QWeakPointer to map items rather than a plain one
Avoids some destructor races by knowing when children have already been cleaned up but are still on various lists. Change-Id: I8b5b01ca2e0bc0eb25fd4abb20a499125fc6ea4c Reviewed-by: Thomas Lowe <thomas.lowe@nokia.com> Reviewed-by: Ian Chen <ian.1.chen@nokia.com> Reviewed-by: Alex Wilson <alex.wilson@nokia.com>
-rw-r--r--src/imports/location/qdeclarativegeomap.cpp41
-rw-r--r--src/imports/location/qdeclarativegeomap_p.h2
2 files changed, 28 insertions, 15 deletions
diff --git a/src/imports/location/qdeclarativegeomap.cpp b/src/imports/location/qdeclarativegeomap.cpp
index 0330d437..de4f07c4 100644
--- a/src/imports/location/qdeclarativegeomap.cpp
+++ b/src/imports/location/qdeclarativegeomap.cpp
@@ -216,8 +216,10 @@ QDeclarativeGeoMap::~QDeclarativeGeoMap()
if (!mapViews_.isEmpty())
qDeleteAll(mapViews_);
// remove any map items associations
- for (int i = 0; i < mapItems_.count(); ++i)
- qobject_cast<QDeclarativeGeoMapItemBase *>(mapItems_.at(i))->setMap(0,0);
+ for (int i = 0; i < mapItems_.count(); ++i) {
+ if (mapItems_.at(i))
+ mapItems_.at(i).data()->setMap(0,0);
+ }
mapItems_.clear();
delete copyrightsWPtr_.data();
@@ -566,10 +568,9 @@ void QDeclarativeGeoMap::mappingManagerInitialized()
// Any map items that were added before the plugin was ready
// need to have setMap called again
- foreach (QObject *obj, mapItems_) {
- QDeclarativeGeoMapItemBase *item = qobject_cast<QDeclarativeGeoMapItemBase *>(obj);
+ foreach (const QWeakPointer<QDeclarativeGeoMapItemBase> &item, mapItems_) {
if (item)
- item->setMap(this, map_);
+ item.data()->setMap(this, map_);
}
}
@@ -1041,7 +1042,12 @@ void QDeclarativeGeoMap::addMapItem(QDeclarativeGeoMapItemBase *item)
QList<QObject *> QDeclarativeGeoMap::mapItems()
{
- return mapItems_;
+ QList<QObject *> ret;
+ foreach (const QWeakPointer<QDeclarativeGeoMapItemBase> &ptr, mapItems_) {
+ if (ptr)
+ ret << ptr.data();
+ }
+ return ret;
}
/*!
@@ -1053,16 +1059,17 @@ QList<QObject *> QDeclarativeGeoMap::mapItems()
\sa mapitems, addMapItem, clearMapItems
*/
-void QDeclarativeGeoMap::removeMapItem(QDeclarativeGeoMapItemBase *item)
+void QDeclarativeGeoMap::removeMapItem(QDeclarativeGeoMapItemBase *ptr)
{
QLOC_TRACE0;
- if (!item || !map_)
+ if (!ptr || !map_)
return;
+ QWeakPointer<QDeclarativeGeoMapItemBase> item(ptr);
if (!mapItems_.contains(item))
return;
updateMutex_.lock();
- item->setParentItem(0);
- item->setMap(0, 0);
+ item.data()->setParentItem(0);
+ item.data()->setMap(0, 0);
// these can be optimized for perf, as we already check the 'contains' above
mapItems_.removeOne(item);
emit mapItemsChanged();
@@ -1083,8 +1090,10 @@ void QDeclarativeGeoMap::clearMapItems()
return;
updateMutex_.lock();
for (int i = 0; i < mapItems_.count(); ++i) {
- qobject_cast<QDeclarativeGeoMapItemBase *>(mapItems_.at(i))->setParentItem(0);
- qobject_cast<QDeclarativeGeoMapItemBase *>(mapItems_.at(i))->setMap(0, 0);
+ if (mapItems_.at(i)) {
+ mapItems_.at(i).data()->setParentItem(0);
+ mapItems_.at(i).data()->setMap(0, 0);
+ }
}
mapItems_.clear();
emit mapItemsChanged();
@@ -1160,9 +1169,13 @@ void QDeclarativeGeoMap::fitViewportToMapItemsRefine(bool refine)
QPointF centerPt;
int itemCount = 0;
for (int i = 0; i < mapItems_.count(); ++i) {
- QDeclarativeGeoMapItemBase *item = qobject_cast<QDeclarativeGeoMapItemBase *>(mapItems_.at(i));
+ if (!mapItems_.at(i))
+ continue;
+ QDeclarativeGeoMapItemBase *item = mapItems_.at(i).data();
// account for the special case - circle
- QDeclarativeCircleMapItem *circleItem = qobject_cast<QDeclarativeCircleMapItem *>(mapItems_.at(i));
+ QDeclarativeCircleMapItem *circleItem =
+ qobject_cast<QDeclarativeCircleMapItem *>(item);
+
if ((!circleItem || !circleItem->center()) && !item)
continue;
if (circleItem && circleItem->center()) {
diff --git a/src/imports/location/qdeclarativegeomap_p.h b/src/imports/location/qdeclarativegeomap_p.h
index 9a13f0a6..bbee3e81 100644
--- a/src/imports/location/qdeclarativegeomap_p.h
+++ b/src/imports/location/qdeclarativegeomap_p.h
@@ -245,7 +245,7 @@ private:
QGeoMap *map_;
QWeakPointer<QDeclarativeGeoMapCopyrightNotice> copyrightsWPtr_;
- QList<QObject *> mapItems_;
+ QList<QWeakPointer<QDeclarativeGeoMapItemBase> > mapItems_;
QMutex updateMutex_;
friend class QDeclarativeGeoMapItem;