Results 1 to 16 of 16

Thread: Posting customeEvent using postEvent crashes Windows but works on Ubuntu Linux

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Posting customeEvent using postEvent crashes Windows but works on Ubuntu Linux

    Thanks for the tip, but I've already tested that. In one version of the program I removed QString* and everything else from the event. It still crashes.
    Note, in version I posted the QString is allocated on the heap and I delete it in the destructor of my over-riding virtual customEvent method. That delete doesn't crash.
    The crash seems to be occurring when the Qt library deletes the event. This crash occurs even if I declare my custom event to be exactly like a standard event.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Posting customeEvent using postEvent crashes Windows but works on Ubuntu Linux

    Quote Originally Posted by cqubed View Post
    Note, in version I posted the QString is allocated on the heap and I delete it in the destructor of my over-riding virtual customEvent method.
    What if one calls setMsg more than once?

    The crash seems to be occurring when the Qt library deletes the event. This crash occurs even if I declare my custom event to be exactly like a standard event.
    Please post a minimal compilable example reproducing the problem.

    BTW. Isn't your thread accessing the main window subclass?
    Last edited by wysota; 20th October 2012 at 12:52.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2012
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Posting customeEvent using postEvent crashes Windows but works on Ubuntu Linux

    Quote Originally Posted by wysota View Post
    What if one calls setMsg more than once?
    Please post a minimal compilable example reproducing the problem. BTW. Isn't your thread accessing the main window subclass?
    If setMsg is called more than once, application will leak memory, but this won't cause crash.
    Is my worker thread accessing main window subclass?
    Quote Originally Posted by cqubed
    I'll check, but don't think so.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Posting customeEvent using postEvent crashes Windows but works on Ubuntu Linux

    Quote Originally Posted by cqubed View Post
    Is my worker thread accessing main window subclass?
    It is.

    Furthermore if you pass a string pointer to the constructor of the event, it will potentially cause a double delete because you are storing a pointer to the string as-is, without making a copy of the string that you later delete in the destructor.

    I suggest you simplify your code as much as possible, then it will be much simpler to find the problem. For instance this:

    Qt Code:
    1. void writeWin(QString *msg) {
    2. if (( m_PtrWorkerThread != NULL) && (QThread::currentThread()== m_PtrWorkerThread)) {
    3. while(m_MustWait) {
    4. #ifdef WIN32
    5. Sleep(1);
    6. #else
    7. usleep(10);//1,000,000 in sec
    8. #endif
    9. }
    10. {
    11. m_MustWait = true;
    12. m_PtrMsg = msg;
    13. PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
    14. ptr_msgEvent->m_ptrPostingThread=QThread::currentThread();
    15. ptr_msgEvent->setMsg(msg);
    16. QCoreApplication::postEvent ( this, ptr_msgEvent);
    17.  
    18. }
    19. } else
    20. {
    21. // m_MustWait = true;
    22. PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
    23. ptr_msgEvent->m_ptrPostingThread=QThread::currentThread();
    24. ptr_msgEvent->setMsg(msg);
    25. QCoreApplication::postEvent ( this, ptr_msgEvent);
    26.  
    27. }
    28. };
    To copy to clipboard, switch view to plain text mode 

    can be written as this (I don't know why you sleep for a couple of seconds here so I ignored that part):

    Qt Code:
    1. void writeWin(const QString& msg) {
    2. PostMessageEvent *ptr_msgEvent = new(PostMessageEvent);
    3. ptr_msgEvent->setMsg(msg);
    4. ptr_msgEvent->m_ptrPostingThread=QThread::currentThread(); // optionally, if you really need it
    5. QCoreApplication::postEvent ( this, ptr_msgEvent);
    6. };
    To copy to clipboard, switch view to plain text mode 

    or better yet just use signals and slots (properly). If you have to "wait", then use a wait condition instead of a busy loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QIcon not shown on Ubuntu, works on Windows
    By JPNaude in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2010, 05:28
  2. Replies: 1
    Last Post: 7th April 2010, 18:13
  3. Works well on Windows but fails on Linux
    By utkuaydin in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2010, 13:22
  4. QPalette works differently on windows and linux
    By babu198649 in forum Newbie
    Replies: 3
    Last Post: 6th March 2008, 07:27
  5. Project won't compile under Windows (works under Linux)
    By philski in forum Qt Programming
    Replies: 7
    Last Post: 14th September 2006, 15:29

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
  •  
Qt is a trademark of The Qt Company.