Hello everybody,
Attached is the latest patch from my BitTorrent branch. I added the new classes SharedData and SharedDataPointer. They are modifications of QSharedData and QSharedDataPointer with the difference that they should be used with polymorphic types.
Changed the base class of Protocols::Generics::Packet::Data to SharedData and implemented the virtual copy ctor to all concrete packet classes. Now the memory problem disappeared.
Regards,
Peter
Peter Dimov wrote:Just figured out where exactly the problem is:
template
Q_OUTOFLINE_TEMPLATE void QSharedDataPointer::detach_helper()
{
T *x = new T(*d);
x->ref.ref();
x = qAtomicSetPtr(&d, x);
if (!x->ref.deref())
delete x;
}
When the private data is detached a copy of it is created (T *x = new T(*d);). The thing is that only the base class part is copied (Protocols::Generics::Packet::Data) instead of copying the derived class. The has no way to know what the correct type is of course.The solution I can think of is to create our own PrivateData and PrivateDataPointer classes, actually modifying the ones from Qt, and adding a pure virtual copy ctor to the data class. The pointer class will call the virtual copy ctor to make sure a correct copy is created.
Any better ideas?
Regards,
Peter
