我已经使用pimpl习惯用法建立了一个共享库。
下面有一些与我面临的问题相关的类:
class PlotPrivate;
class RESTAPILIB_EXPORT Plot : public IFarmObject
{
using IFarmObject::IFarmObject;
Q_DISABLE_COPY(Plot)
Q_OBJECT
...
Q_PROPERTY(QList<QGeoCoordinate> coordinates READ coordinates WRITE setCoordinates NOTIFY coordinatesChanged)
...
public:
void setCoordinates(const QList<QGeoCoordinate> &coordinates);
QList<QGeoCoordinate> coordinates() const;
Q_INVOKABLE QVariantList coordinatesQml() const;
QPolygonF coordinatesPolygon() const;
...
signals:
void coordinatesChanged(QList<QGeoCoordinate> coordinates);
private:
Q_DECLARE_PRIVATE(Plot)
};
...
class PlotPrivate : public IFarmObjectPrivate
{
public:
PlotPrivate(Plot *q, const QString &id, IObject::ObjectType type = IObject::PLOT);
...
public:
QList<QGeoCoordinate> m_coordinates;
Q_DECLARE_PUBLIC(Plot)
};
...
Plot::Plot(const QString &id)
: IFarmObject(new PlotPrivate(this, id))
{
}
...
void Plot::setCoordinates(const QList<QGeoCoordinate> &coordinates)
{
Q_D(Plot);
d->m_coordinates = coordinates;
d->m_area = 0.0;
if (d->m_coordinates.size() > 2)
d->m_area = abs(getArea(coordinatesPolygon()));
//TODO: render m_image
emit coordinatesChanged(d->m_coordinates);
emit areaChanged(d->m_area);
}
...
const QList<QGeoCoordinate> GeoCoordinatesModel::coordinates() const
{
QList<QGeoCoordinate> lst;
if (m_lst.size() > 2)
{
foreach (GeoCoordinateEntry *e, m_lst) {
lst << e->coordinate();
}
}
return lst;
}
...
void wgFarmPlotEditor::savePlot(Plot *plot)
{
Plot *p = new Plot(plot);
QList<QGeoCoordinate> coords = p_coordinatesModel->coordinates();
p->setCoordinates(coords);
newRequestUpdt(p);
p->deleteLater();
p_coordinatesModel->resetModel();
}
在共享库中,Plot是一个指针。 调用插槽wgFarmPlotEditor :: savePlot()时,我的应用程序在以下部分崩溃: p-> setCoordinates(coords); 我试图了解为什么会这样。 我相信与隐式共享有关,但是我不知道为什么。 有什么想法吗?
注意:为了更好地阅读,我隐藏了部分代码。
注2:IFarmObject和IFarmObjectPrivate都从QObject继承,也都有对应的d_ptr和q_ptr对象。
注3:我按照该指南编写了类:https://wiki.qt.io/D-Pointer
注4:wgFarmPlotEditor类 在另一个共享库中。.不确定是否有区别。
下面是崩溃的堆栈跟踪:
1 QGenericAtomicOps<QAtomicOpsBySize<4>>::load<int> qgenericatomic.h 90 0xf2e29d6
2 QBasicAtomicInteger<int>::load qbasicatomic.h 103 0xf2e5dc0
3 QtPrivate::RefCount::deref qrefcount.h 66 0xf2e4701
4 QList<QGeoCoordinate>::~QList<QGeoCoordinate> qlist.h 826 0xf3215d0
5 QList<QGeoCoordinate>::operator= qlist.h 506 0xf3216e5
6 Plot::setCoordinates plot.cpp 40 0xf31f463
7 wgFarmPlotEditor::savePlot wgfarmploteditor.cpp 77 0x3b66417
8 wgFarmPlotEditor::qt_static_metacall moc_wgfarmploteditor.cpp 98 0x3b7341e
9 wgFarmPlotEditor::qt_metacall moc_wgfarmploteditor.cpp 154 0x3b7335a
10 QMetaObject::metacall qmetaobject.cpp 302 0x665b7780
11 QQmlObjectOrGadget::metacall qqmlpropertycache.cpp 1733 0x429eaa1
12 CallMethod qv4qobjectwrapper.cpp 1177 0x41abe23
13 CallPrecise qv4qobjectwrapper.cpp 1437 0x41ac7bc
14 QV4::QObjectMethod::callInternal qv4qobjectwrapper.cpp 1975 0x41aa65a
15 QV4::QObjectMethod::call qv4qobjectwrapper.cpp 1913 0x41aa2de
16 QV4::FunctionObject::call qv4functionobject_p.h 163 0x3fd0714
17 QV4::Runtime::method_callProperty qv4runtime.cpp 1062 0x41d06a6
18 QV4::Moth::VME::exec qv4vme_moth.cpp 800 0x41bfd32
19 QV4::Moth::VME::exec qv4vme_moth_p.h 72 0x3fd5ad0
20 QV4::Function::call qv4function_p.h 72 0x3fd06d0
... <More>