extragear/multimedia/amarok/src

Ian Monroe ian.monroe at gmail.com
Fri Feb 20 18:32:25 CET 2009


On Fri, Feb 20, 2009 at 11:11 AM, Ian Monroe <ian.monroe at gmail.com> wrote:
> On Fri, Feb 20, 2009 at 11:04 AM, Mark Kretschmann <kretschmann at kde.org> wrote:
>> SVN commit 929114 by markey:
>>
>> Beginnings of SmartPointerList class:
>> A QList for storing pointers to QObjects, that automatically removes
>> the pointers when objects are deleted.
>>
>> The evil scheme behind this idea is to fix many of our crashes that
>> result from storing (caching) QLists of pointers to QActions in various
>> places in Amarok. The problem is that sometimes these QActions get
>> deleted, and we end up with dangling pointers in these lists. That's
>> causing crashes like in BR 184630, when we dereference these dangling
>> pointers.
>>
>> Now my class does have a few drawbacks still, which may be fixable with
>> more tweaking, or maybe not:
>>
>> * Does not work with Qt's foreach(), as it subclasses QObject. You need
>>  to iterate over the list in traditional ways.
>
> foreach works on all the lists in Qt, even though they don't derive
> from each other. All you have to do is figure out what methods it
> needs implemented. Probably a matter of just keeping the foreach and
> seeing what errors gcc gives you.

I found the code:
#define Q_FOREACH(variable, container)                                \
for (QForeachContainer<__typeof__(container)> _container_(container); \
     !_container_.brk && _container_.i != _container_.e;              \
     __extension__  ({ ++_container_.brk; ++_container_.i; }))
              \
    for (variable = *_container_.i;; __extension__
({--_container_.brk; break;}))

#else

struct QForeachContainerBase {};

template <typename T>
class QForeachContainer : public QForeachContainerBase {
public:
    inline QForeachContainer(const T& t): c(t), brk(0), i(c.begin()),
e(c.end()){};
    const T c;
    mutable int brk;
    mutable typename T::const_iterator i, e;
    inline bool condition() const { return (!brk++ && i != e); }
};


It needs a begin(), an end() and a SmartPointerList::const_iterator.

Are you sure you couldn't just derive from QList and QObject though?

Ian


More information about the Amarok-devel mailing list