I am trying to track a memory leak. I know it is in some fine point of Qt, but I am not sure exactly what or how to resolve...though that'll be determined by the cause...

I'm using Qt 4.4.3 Commercial under Windows. I've only tested under Windows; but there is nothing Windows specific in the code - it's all just Qt and C++, compiled under VS2008.

Basic problem:

I have a network data source that pushes data out to a QDialog directed class via a signal. The signal's parameter is a const QByteArray reference.

The dialog turns around and calls a widget's function to pass the same data (in the same manner) to the child widget. (So far, so good - no leak.)

The child widget, upon receiving the data allocates an object that can understand the data (it does not allocate anything), pushes it into a QList, and calls repaint(). The goal of the child widget is to ultimately draw something based on the data during the paintEvent().

So now here comes the problem:

If I immediately delete the data, no leak. Even if I put it into the QList and immediately take it out there is no memory leak.

However, only the paintEvent (at this point) knows when the data has been used and can properly dispose of it. I can verify under the debugger that the data does get consumed and deleted - using the QObjectCleanUpHandler object. But if I let it operate in this mode, then the memory use skyrockets as if the data is not getting deleted.

Stepping through using the debugger shows that the QList grows no larger than 1 during a run - so I know everything is getting consumed - and I can see QObjectCleanUpHandler grow until I let it run for a bit, at which point if I do a debug break again I can see that both the QObjectCleanUpHandler and the QList are empty (when it goes to push more data into the QList).

At this point, I've removed a lot of the code (commetted it out) to minimize anything that happens, and I can verify that the memory leak only happens if I try to clean up during the paintEvent() of the child widget.

So my question is basically:

Is there a reason I cannot delete stuff during the paintEvent? Or is there some other fine point in Qt that I am running into?

If it's just that I can't delete stuff during the paintEvent, then I guess I could pull it from the primary QList, use it, and then push it into another QList that I could clean up on a regular basis; but I'd rather just clean it up when I'm done with it in the paintEvent - I don't need it any more after that any way.

Advice greatly appreciated...

Ben