PDA

View Full Version : Passing data in PostEvent



marcelo.emmerich
1st September 2016, 19:44
why is this not working? I do the usual PostEvent call to update the UI from another thread. It works perfectly until I comment free(buffer) back in. The destructor is called by the system (I assume after event() has consumed the event) but trying to free the buffer causes an access violation. leaving it out causes memory leaks.


class FrameEvent : public QEvent
{
public:
FrameEvent() : QEvent(QEvent::Type::User) {}

~FrameEvent()
{
free(buffer);
}
unsigned char * buffer;
};

anda_skoa
2nd September 2016, 10:26
How was the buffer allocated?

Does it also get freed elsewhere?

Cheers,
_

Lesiok
2nd September 2016, 12:02
I suggest using QPointer instead of the standard pointer. It is much safer.

anda_skoa
2nd September 2016, 13:56
I suggest using QPointer instead of the standard pointer. It is much safer.
You probably meant QSharedPointer or QScopedPointer, as QPointer is for QObjects.

For a byte array it might even be better to go for QByteArray though.

It still depends on how the memory is allocated or used in the first place.
E.g. memory allocated with "new" must be free with "delete", while memory allocated with "malloc" must be freed with "free", etc.

Cheers,
_

Lesiok
2nd September 2016, 14:10
You probably meant QSharedPointer or QScopedPointer, as QPointer is for QObjects.

For a byte array it might even be better to go for QByteArray though.
Of course.