Does Qt allready have its own new and delete operators?

自古美人都是妖i 提交于 2019-12-24 04:40:12

问题


I'm using a QGraphicsScene widget and showing upon it some points as QGraphicsRectItem.

This means calling lots of new + addItem() when showing up, and removeItem() + delete to get rid of unused points.

Of course, for performance issues, I've implemented my own new and delete operators, which basically recycle pre-allocated memory chunks.

Those operators works very well with anything, except for Qt classes (I mean QObject derived classes). The error raised is different every time but always occurs during an internal call after all my slot functions have been executed with no errors.

I also tried them with some instances of QWidget added to some layouts etc.

So the question is, does Qt allready have its own new and delete operators, which I am totally skipping defining my own ones?


回答1:


It's trivial enough to inspect Qt's headers to ascertain whether such custom operators exist. They don't.

The QGraphicsRectItem does not derive from QObject, so that's not an issue anyway. You're not getting as much savings from that as you think you are, since Qt uses the PIMPL idiom extensively, and QGraphicsItem is allocating its PIMPL on the heap.

You'd also need to show the implementation of your operator - for all I know, you're not maintaining proper alignment. What compiler(s) are you targeting? What version of Qt?

To do what you intend, you must modify Qt. Declare and implement:

void* QGraphicsItem::operator new (size_t sz)
void* QGraphicsItem::operator new[] (std::size_t count)
void QGraphicsItem::operator delete (void* ptr, std::size_t sz)
void QGraphicsItem::operator delete[] (void* ptr, std::size_t sz)
void* QGraphicsItemPrivate::operator new (size_t sz)
void QGraphicsItemPrivate::operator delete (void* ptr, std::size_t sz)

Make sure that your allocator properly aligns the storage.



来源:https://stackoverflow.com/questions/25931668/does-qt-allready-have-its-own-new-and-delete-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!