[Kde-pim] SEGV reading Akonadi::Attribute from retrieveItem

Jesús Pérez kdelists at chuso.net
Wed May 28 10:53:29 BST 2014


Hi, I'm learning to develop Akonadi resources.

Everything is going well until I set a custom attribute in collection
and I try to read this attribute in retrieveItem() getting a
segmentation violation for trying to access a null pointer.

This is how I did it:

I extend Akonadi::Attribute class to implement my custom property.
Then, I register my custom attribute in resource constructor:

testResource::testResource( const QString &id )
  : ResourceBase( id )
{
  // ...
  AttributeFactory::registerAttribute<TestAttribute>();
}

When retrieving collections, I set this attribute to collection:

void testResource::retrieveCollections()
{
  Collection c;
  // ...
  TestAttribute *attr = new TestAttribute("value1");
  c.addAttribute(attr);
}

I've checked that I can successfully read this value from retrieveItems:

void testResource::retrieveItems( const Akonadi::Collection &collection )
{
  TestAttribute *attr = collection.attribute<TestAttribute>();
  QByteArray attr_value = attr->serialized();
  qDebug("retrieveItems value: %s", attr_value.constData());
  // ...
}

But when trying to get attribute from retrieveItem, it gives a null
pointer. So trying to read any class attribute will give a SEGV:

bool testResource::retrieveItem( const Akonadi::Item &item, const
QSet<QByteArray> &parts )
{
  TestAttribute *attr =
item.parentCollection().attribute<TestAttribute>(); // NULL pointer
  QByteArray attr_value = attr->serialized(); // SEGV
  qDebug("retrieveItem value: %s", attr_value.constData());
}

I've also tried copying attribute from collection to item in
retrieveItems and then read item attribute in retrieveItem with same
result:

void testResource::retrieveItems( const Akonadi::Collection &collection )
{
  TestAttribute *attr = collection.attribute<TestAttribute>();
  // ...
  foreach (Akonadi::Item item, itemList)
    item.addAttribute(attr);
  // ...
}

bool testResource::retrieveItem( const Akonadi::Item &item, const
QSet<QByteArray> &parts )
{
  TestAttribute *attr = item.attribute<TestAttribute>(); // NULL pointer
  QByteArray attr_value = attr->serialized(); // SEGV
  qDebug("retrieveItem value: %s", attr_value.constData());
}

In both cases, I've checked with akonadiconsole that attribute is
successfully set, so the problem seems to be accessing to it from
retrieveItem.

I've made a full test project by taking sample agent available at
http://techbase.kde.org/Development/Tutorials/Akonadi/Resources and
adding custom attribute:

http://anongit.chuso.net/testresource/tree/testresource.cpp#n48

Also, the second case adding attribute to item:

http://anongit.chuso.net/testresource/tree/testresource.cpp?h=attribute-on-items#n48

What am I doing wrong? Am I trying to do something that is not possible to do?
_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/



More information about the kde-pim mailing list