Results 1 to 3 of 3

Thread: Access Violation on Signal Emit

  1. #1
    Join Date
    Sep 2007
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Access Violation on Signal Emit

    Hi,

    I have been banging my head on this issue for days now. I have a
    QFrame which is using a QHBoxLayout. The frame contains one widget, a
    custom class inheriting from QTextBrowser. The setup code looks as
    follows:

    -------------------------------------------------------------------------
    Qt Code:
    1. int main(int argc, char *argv[]){
    2. int retval = 0;
    3. QApplication application(argc, argv);
    4. QFrame *mainFrame = new QFrame(NULL, 0) ;
    5. QHBoxLayout *frameLayout = new QHBoxLayout() ;
    6. Controller *controller = new Controller(NULL,
    7. MainFrame) ;
    8. TextBrowserWidget *textBrowserWidget = new
    9. TextBrowserWidget(controller, NULL, 0) ;
    10.  
    11. frameLayout->addWidget(textBrowserWidget) ;
    12. mainFrame->setLayout(frameLayout) ;
    13. controller->ActionSlot(1, QString("Testing")) ;
    14. mainFrame->showMaximized() ;
    15. retval = application.exec() ;
    16. return retval;
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    The notion of the ActionSlot of the controller is to "change the
    state" of the QFrame. For this simple example, "changing state" is
    simply removing the textBrowserWidget from the frame and replacing it
    with a new instance as follows (_mainFrame here is the QFrame from
    main above):

    -------------------------------------------------------------------------
    Qt Code:
    1. void Controller::ActionSlot(int action, QVariant parameter){
    2. QHBoxLayout *layout = (QHBoxLayout *)_mainFrame->layout() ;
    3. while(layout->count() > 0){
    4. QLayoutItem *item = layout->itemAt(0) ;
    5. if(item){
    6. QWidget *widget = item->widget() ;
    7. if(widget){
    8. delete widget;
    9. }
    10. }
    11. }
    12. TextBrowserWidget *newBrowser = new TextBrowserWidget(this, NULL,
    13. 0) ;
    14. layout->addWidget(newBrowser) ;
    15. fclose(handle) ;
    16. }
    To copy to clipboard, switch view to plain text mode 
    -------------------------------------------------------------------------

    Now this works well for the first instance of ActionSlot called
    explicitly in main above, however I also have the anchorTriggered
    signal of QTextBrowser attached to a custom slot in my
    TextBrowserWidget class (inheriting from QTextBrowser) so that when a
    link in the TextBrowserWidget is clicked, it emits a more
    "descriptive" custom signal as follows:

    -------------------------------------------------------------------------
    Qt Code:
    1. .
    2. .
    3. .
    4. QObject::connect(
    5. this,
    6. SIGNAL(anchorClicked(QUrl)),
    7. this,
    8. SLOT(linkClicked(QUrl))
    9. ) ;
    10. QObject::connect(
    11. this,
    12. SIGNAL(linkClickedSignal(int, QVariant)),
    13. _controller,
    14. SLOT(ActionSlot(int, QVariant))
    15. ) ;
    16. .
    17. .
    18. .
    19. void TextBrowserWidget::linkClicked(QUrl url){
    20. emit linkClickedSignal(1, QString("Testing..."));
    21. }
    To copy to clipboard, switch view to plain text mode 
    -------------------------------------------------------------------------

    For some reason, as I mentioned the explicit call to ActionSlot from
    main() completes successfully. However when I click on one of the
    links in the TextBrowserWidget, I receive "0xC0000005: Access
    violation writing location 0xfeeefeee." From the stack trace below
    this emanates
    up from QString's operator "=":

    -------------------------------------------------------------------------
    [CODE]
    QtCored4.dll!q_atomic_decrement(volatile int * ptr=0xfeeefeee) Line
    152 + 0x9 bytes C++
    QtCored4.dll!QBasicAtomic::deref() Line 76 + 0x10 bytes C++
    QtCored4.dll!QString::operator=(const QString & other={...}) Line
    1063 + 0x8 bytes C++
    QtGuid4.dll!QTextControlPrivate::mouseReleaseEvent (Qt::MouseButton
    button=LeftButton, const QPointF & pos={...}) Line 1578 + 0x29 bytes C
    ++
    QtGuid4.dll!QTextControl::processEvent(QEvent * e=0x0012c4d0, const
    QMatrix & matrix={...}, QWidget * contextWidget=0x00a077a8) Line 864 C
    ++
    QtGuid4.dll!QTextControl::processEvent(QEvent * e=0x0012c4d0, const
    QPointF & coordinateOffset={...}, QWidget * contextWidget=0x00a077a8)
    Line 812 C++
    QtGuid4.dll!QTextEditPrivate::sendControlEvent(QEv ent * e=0x0012c4d0)
    Line 78 + 0x57 bytes C++
    QtGuid4.dll!QTextEdit::mouseReleaseEvent(QMouseEve nt * e=0x0012c4d0)
    Line 1466 C++
    QtGuid4.dll!QTextBrowser::mouseReleaseEvent(QMouse Event *
    e=0x0012c4d0) Line 926 C++
    QtGuid4.dll!QWidget::event(QEvent * event=0x0012c4d0) Line 6064 C++
    QtGuid4.dll!QFrame::event(QEvent * e=0x0012c4d0) Line 641 C++
    QtGuid4.dll!QAbstractScrollArea::viewportEvent(QEv ent * e=0x0012c4d0)
    Line 903 + 0xc bytes C++
    QtGuid4.dll!QAbstractScrollAreaPrivate::viewportEv ent(QEvent *
    event=0x0012c4d0) Line 78 + 0x28 bytes C++
    QtGuid4.dll!QAbstractScrollAreaFilter::eventFilter (QObject *
    o=0x00a077a8, QEvent * e=0x0012c4d0) Line 89 + 0x29 bytes C++
    QtGuid4.dll!QApplicationPrivate::notify_helper(QOb ject *
    receiver=0x00a077a8, QEvent * e=0x0012c4d0) Line 3533 + 0x1b bytes C+
    +
    QtGuid4.dll!QApplication::notify(QObject * receiver=0x00a077a8, QEvent
    * e=0x0012c4d0) Line 3240 + 0x2f bytes C++
    QtCored4.dll!QCoreApplication::notifyInternal(QObj ect *
    receiver=0x00a077a8, QEvent * event=0x0012c4d0) Line 516 C++
    QtCored4.dll!QCoreApplication::sendSpontaneousEven t(QObject *
    receiver=0x00a077a8, QEvent * event=0x0012c4d0) Line 188 + 0x38 bytes
    C++
    QtGuid4.dll!QETWidget::translateMouseEvent(const tagMSG & msg={...})
    Line 2757 + 0x14 bytes C++
    QtGuid4.dll!QtWndProc(HWND__ * hwnd=0x004e0572, unsigned int
    message=514, unsigned int wParam=0, long lParam=4719072) Line 1390 +
    0xc bytes C++
    user32.dll!7e418734()
    [Frames below may be incorrect and/or missing, no symbols loaded for
    user32.dll]
    user32.dll!7e418816()
    user32.dll!7e4189cd()
    user32.dll!7e418a10()
    QtCored4.dll!QEventDispatcherWin32::processEvents( QFlags<enum
    QEventLoop::ProcessEventsFlag> flags={...}) Line 504 + 0x18 bytes C++
    QtGuid4.dll!QGuiEventDispatcherWin32::processEvent s(QFlags<enum
    QEventLoop::ProcessEventsFlag> flags={...}) Line 975 + 0x15 bytes C++
    QtCored4.dll!QEventLoop::processEvents(QFlags<enum
    QEventLoop::ProcessEventsFlag> flags={...}) Line 125 C++
    QtCored4.dll!QEventLoop::exec(QFlags<enum
    QEventLoop::ProcessEventsFlag> flags={...}) Line 170 + 0x2d bytes C++
    QtCored4.dll!QCoreApplication::exec() Line 735 + 0x15 bytes C++
    QtGuid4.dll!QApplication::exec() Line 3039 C++
    TextBrowserTester.exe!main(int argc=1, char * * argv=0x003e7898) Line
    15 + 0x6 bytes C++
    TextBrowserTester.exe!WinMain(HINSTANCE__ * instance=0x00400000,
    HINSTANCE__ * prevInstance=0x00000000, char * __formal=0x00151f16, int
    cmdShow=1) Line 103 + 0x12 bytes C++
    TextBrowserTester.exe!__tmainCRTStartup() Line 578 + 0x35 bytes C

    TextBrowserTester.exe!WinMainCRTStartup() Line 403 Ckernel32.dll!
    7c816fd7()
    [/CODE\]
    -------------------------------------------------------------------------

    My question really is does anyone out there see anything obvious as to
    why the access violation is occuring?? Is something being deleted
    twice here and I am just not seeing it?? Can I even remove widgets
    from a layout this way using delete()?? Is the code I have posted here
    just plain wrong?? I have also tried layout->removeItem(), layout-
    >removeWidget() to try to remove them from the layout, and surely

    layout->count() decrements, but the widget is still visible and
    usable. I would rather not use QStackedWidget because I don't want to
    leave widgets hanging around wasting resources as there could be a
    large number of them if left uncleaned.

    Thanks Much

  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: Access Violation on Signal Emit

    You can't delete an object that is the sender of the currently processed signal and from what I understand that's what you are trying to do. Use deleteLater() instead of delete.

  3. #3
    Join Date
    Sep 2007
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Access Violation on Signal Emit

    Thank you SOOOOOOO much. deleteLater() works perfectly.

Similar Threads

  1. how to emit signal in a static function ?
    By cxl2253 in forum Qt Programming
    Replies: 32
    Last Post: 7th July 2016, 21:36
  2. Signal defined in "a.h" can not emit in "b.cpp"
    By Shawn in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 16:55
  3. From extends QTreeWidgetItem emit signal?
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 19th May 2006, 14:54
  4. Replies: 4
    Last Post: 18th May 2006, 17:48
  5. emit a signal
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2006, 11:14

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.