fix(src/components/contacts/ContactsListModel): set parent of contacts (avoid leaks)

This commit is contained in:
Ronan Abhamon 2017-03-24 10:33:04 +01:00
parent e66f46f318
commit 9a269a14fe
3 changed files with 7 additions and 9 deletions

View file

@ -30,7 +30,7 @@ using namespace std;
// =============================================================================
ContactModel::ContactModel (shared_ptr<linphone::Friend> linphone_friend) {
ContactModel::ContactModel (QObject *parent, shared_ptr<linphone::Friend> linphone_friend) : QObject(parent) {
m_linphone_friend = linphone_friend;
m_vcard = make_shared<VcardModel>(linphone_friend->getVcard());
@ -38,7 +38,7 @@ ContactModel::ContactModel (shared_ptr<linphone::Friend> linphone_friend) {
m_linphone_friend->setData("contact-model", *this);
}
ContactModel::ContactModel (VcardModel *vcard) {
ContactModel::ContactModel (QObject *parent, VcardModel *vcard) : QObject(parent) {
Q_ASSERT(vcard != nullptr);
QQmlEngine *engine = App::getInstance()->getEngine();

View file

@ -40,8 +40,8 @@ class ContactModel : public QObject {
friend class SmartSearchBarModel;
public:
ContactModel (std::shared_ptr<linphone::Friend> linphone_friend);
ContactModel (VcardModel *vcard);
ContactModel (QObject *parent, std::shared_ptr<linphone::Friend> linphone_friend);
ContactModel (QObject *parent, VcardModel *vcard);
~ContactModel () = default;
std::shared_ptr<VcardModel> getVcardModel () const {

View file

@ -37,13 +37,11 @@ ContactsListModel::ContactsListModel (QObject *parent) : QAbstractListModel(pare
// Init contacts with linphone friends list.
for (const auto &friend_ : m_linphone_friends->getFriends()) {
ContactModel *contact = new ContactModel(friend_);
ContactModel *contact = new ContactModel(this, friend_);
// See: http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership
// The returned value must have a explicit parent or a QQmlEngine::CppOwnership.
App::getInstance()->getEngine()->setObjectOwnership(
contact, QQmlEngine::CppOwnership
);
App::getInstance()->getEngine()->setObjectOwnership(contact, QQmlEngine::CppOwnership);
addContact(contact);
}
@ -100,7 +98,7 @@ bool ContactsListModel::removeRows (int row, int count, const QModelIndex &paren
// -----------------------------------------------------------------------------
ContactModel *ContactsListModel::addContact (VcardModel *vcard) {
ContactModel *contact = new ContactModel(vcard);
ContactModel *contact = new ContactModel(this, vcard);
App::getInstance()->getEngine()->setObjectOwnership(contact, QQmlEngine::CppOwnership);
qInfo() << "Add contact:" << contact;