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

    Cool Posting customeEvent using postEvent crashes Windows but works on Ubuntu Linux

    I have a simple Qt application with two threads a main thread and a worker thread. The main thread supports a QApplication, QMainWindow. The QMainWindow has a textEdit widget. I have sub-classed QEvent to create a MyCustomEvent. MyCustomEvent has a QString member. I have overridden "customEvent" in the QMainWindow object. My "customEvent" method uses insertPlainText to insert the text contained in the QString into the textEdit widget.

    I am attempting use"QCoreApplication:: postEvent to post "MyCustomEvent"s from both the worker thread and the main thread.

    My problem is: This works really nicely when the program is compiled and run on Ubuntu Linux. It crashes on every exit from my customEvent mentod when compiled and run as a 32-bit windows app from Visual Studion 2010 Qt add-in on 64 bit Windows 7.

    The crash occurs after the overridden customEvent method is called. It occurs immediately after the destructor for MyCustomEvent is called. The crash occurs in PostMessageEvent::~PostMessageEvent() short after PostMessageEvent::'scalar deleting destructor', operator delete:, _RTC_CheckEsp.

    I have checked my allocation of MyCustomeEvent carefully. It is allocated on the heap, NOT on the stack. The QString it contains is allocated on the heap and is deleted by ~MyCustomEvent.

    Note I originally tried using cross thread slots and signals to achieve the communication between the worker thread and gui thread, but again on Windows it crashed every time. I switched to posted events to try and get a working app.

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

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

    What does the worker thread do?
    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

    The worker thread reads a file (Rockwell PLC program file in L5K format), parses it and writes an output file.
    postEvent crashes customEvent handler even when called from gui thread.

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

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

    Can we see the event handler? Are you sure you are not deleting the event object yourself?
    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.


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

    Default Re: Posting customeEvent .....here is stripped down source code

    To anyone who might be able to help.
    Here is the code.

    I repeat, this runs fine when build under Ubuntu Linux.
    It givers SIGSEGV on first exit from customEvent when built on Windows 7 64 bit machine as a 32 bit windows app using Visual Studio 2010, Qt OpenSource 4.8.3, Qt Visual Studio-Addin 1.1.11.

    The SIGSEGV comes from the event destructor, but I can't figure out why its happening and why it doesn't happen on Linux.
    Attached Files Attached Files

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

    Default Re: Posting customeEvent .....here is stripped down source code

    Why are you storing a pointer to a string in the event? Why not a real object? There is a good chance that if you correct this, your program will stop crashing.
    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.


  7. #7
    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


  9. #9
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    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.


  11. #11
    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

    I resolved the problem.
    Issue was caused by Input property on Visual Studio 2010 property page being set incorrectly. Somehow I had mixed Windows Qt debug libraries and non-debug libraries in the same link. When I cleaned this up, ie. used all debug libraries in debug configuration and all non-debug libraries in release configuration my program started running perfectly in WOW64. I'm a little mystified. I can see how mixing libraries might cause a crash but I'm somewhat mystified as to why it worked in XP and other Win-32 environments and crashed in WOW64.
    Anyway it was definitely my problem and not a Windows or Qt problem. So far I'm very impressed with cross platform features of Qt. Its a great framework.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.