Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, July 2007
Is it necessary to delete allocated QCustomEvent object manually????


Message 1 in thread

hi,



I am sending the event in a thread for painting widget in a slot, like

QCustomEvent *e=new QCustomEvent(10000);
QApplication::postEvent(fm,&e);

As i am not deleting  e (deallocating memory of e), because when i use

delete e

in a custom event slot,it will give me segmentation fault....

Now i am not deleting e, and its working ok without any problem....

But as i am creating object of QCustomEvent, in a thread everytime when i 
paint widget.... can u tell me plz...will it give me problem in a future as i 
am not deallocating memory....


Thanks and Regards,
Niranjan. 
 

--
 [ signature omitted ] 

Message 2 in thread

Hi,

IIRC, according to the docs you MUST NOT delete QEvent-Objects because the 
event-handling subsystem will take ownership and asure that processed 
events will be deleted.

Regards,
Malte

--
 [ signature omitted ] 

Message 3 in thread

On Jul 16, 2007, at 8:12 AM, Niranjan wrote:

> hi,
>
>
>
> I am sending the event in a thread for painting widget in a slot, like
>
> QCustomEvent *e=new QCustomEvent(10000);
> QApplication::postEvent(fm,&e);
>
> As i am not deleting  e (deallocating memory of e), because when i use
>
> delete e
>
> in a custom event slot,it will give me segmentation fault....
>
> Now i am not deleting e, and its working ok without any problem....
>
> But as i am creating object of QCustomEvent, in a thread everytime  
> when i
> paint widget.... can u tell me plz...will it give me problem in a  
> future as i
> am not deallocating memory....
>
>
> Thanks and Regards,
> Niranjan.

According to the documentation, posted events are "taken over" and  
deleted after processing.

see:

http://doc.trolltech.com/4.3/qcoreapplication.html#postEvent

or

http://doc.trolltech.com/3.3/qapplication.html#postEvent

depending on what version of Qt you are using (i'm guesing that it's  
Qt 3 becouse of "QCustomEvent" class

"The event must be allocated on the heap since the post event queue  
will take ownership of the event and delete it once it has been posted."

Also you have error in your code snippet

QCustomEvent *e = new QCustomEvent(10000);
QApplication::postEvent(fm, e);

or it should be:

QCustomEvent e(10000);
QApplication::sendEvent(fm, &e);

It depends if you want to wait for event being processed, or if you  
want to queue it.

--
 [ signature omitted ]