问题
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