Results 1 to 5 of 5

Thread: Passing data in PostEvent

  1. #1
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X

    Default Passing data in PostEvent

    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.

    Qt Code:
    1. class FrameEvent : public QEvent
    2. {
    3. public:
    4. FrameEvent() : QEvent(QEvent::Type::User) {}
    5.  
    6. ~FrameEvent()
    7. {
    8. free(buffer);
    9. }
    10. unsigned char * buffer;
    11. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing data in PostEvent

    How was the buffer allocated?

    Does it also get freed elsewhere?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing data in PostEvent

    I suggest using QPointer instead of the standard pointer. It is much safer.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Passing data in PostEvent

    Quote Originally Posted by Lesiok View Post
    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,
    _

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Passing data in PostEvent

    Quote Originally Posted by anda_skoa View Post
    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.

Similar Threads

  1. Passing data between MainWindow and my class
    By r2com in forum Qt Programming
    Replies: 10
    Last Post: 27th July 2016, 17:04
  2. Need help passing data to qml
    By brasscs in forum Qt Programming
    Replies: 7
    Last Post: 22nd April 2015, 18:10
  3. passing data from forms
    By shreeram in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2015, 16:37
  4. Replies: 0
    Last Post: 14th April 2011, 16:10
  5. Passing data to a QThread
    By lynchkp in forum Newbie
    Replies: 4
    Last Post: 10th March 2011, 23:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.