Results 1 to 7 of 7

Thread: Devision By Zero Error while emitting signal

  1. #1
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Devision By Zero Error while emitting signal

    Hi,

    I have sort of a big problem: my qt app is more or less randomly throwing a "integer division by zero"-error while processing an emitted signal (the function created in the moc'd cpp file).

    This is the situation:
    - Qt App with a QGraphicsView containing one PixmapItem that repeatedly gets updated and one QDeclarativeComponent for the UI that overlays the PixmapItem.
    - I have one QThread that collects data from a picture grabbing device and once finished grabbing enough data it emits a "onNewImage()" signal without any parameters.
    - the according slot "newImage()" in my MainWindow then creates a QImage/QPixmap from the data (BYTE *) and gives this image to the PixmapItem

    In general, this work very much to my satisfaction.

    But now an then, an more or less reproducible (3 out of 10), my app gets an "integer devision by zer"-error an crashes:

    This error is thrown INSIDE the handling of the emitted signal in the according moc'd cpp file:

    This is what the debugger-log says:


    FYI: I do not even have one division in the whole code ...

    And here some snippets:
    - emitting function in Thread:
    Qt Code:
    1. void GrabberController::run() {
    2.  
    3. if(!_inited)
    4. return;
    5. _fpsTimer.start();
    6.  
    7. _running = true;
    8.  
    9. while(_running) {
    10. if(_grabber->Download(_data)) {
    11. _fps++;
    12. emit onNewImage();
    13. }
    14. else
    15. qDebug(_grabber->GetLastError().c_str());
    16.  
    17. }
    18.  
    19. _running = false;
    20. }
    21.  
    22. with:
    23.  
    24. signals:
    25. void onNewImage();
    To copy to clipboard, switch view to plain text mode 

    - the receiving end:
    Qt Code:
    1. void MainWindow::newImage() {
    2.  
    3. QSize size = _grabber->imageSize();
    4. _currentImage = QImage(_grabber->data(), size.width(), size.height(), _grabber->imageFormat());
    5. if(_currentImage.isNull())
    6. return;
    7.  
    8. //_imgItem.setImage(_currentImage);
    9. _pixItem.setPixmap(QPixmap::fromImage(_currentImage));
    10. _graphicsView->update();
    11. }
    12.  
    13. with:
    14. connect(_grabber, SIGNAL(onNewImage()), SLOT(newImage()), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    - and finaly the error throwing moc'd cpp:
    Qt Code:
    1. /****************************************************************************
    2. ** Meta object code from reading C++ file 'grabbercontroller.h'
    3. **
    4. ** Created: Mon 10. Oct 13:28:02 2011
    5. ** by: The Qt Meta Object Compiler version 62 (Qt 4.7.4)
    6. **
    7. ** WARNING! All changes made in this file will be lost!
    8. *****************************************************************************/
    9.  
    10. #include "../grabbercontroller.h"
    11. #if !defined(Q_MOC_OUTPUT_REVISION)
    12. #error "The header file 'grabbercontroller.h' doesn't include <QObject>."
    13. #elif Q_MOC_OUTPUT_REVISION != 62
    14. #error "This file was generated using the moc from 4.7.4. It"
    15. #error "cannot be used with the include files from this version of Qt."
    16. #error "(The moc has changed too much.)"
    17. #endif
    18.  
    19. QT_BEGIN_MOC_NAMESPACE
    20. static const uint qt_meta_data_GrabberController[] = {
    21.  
    22. // content:
    23. 5, // revision
    24. 0, // classname
    25. 0, 0, // classinfo
    26. 3, 14, // methods
    27. 0, 0, // properties
    28. 0, 0, // enums/sets
    29. 0, 0, // constructors
    30. 0, // flags
    31. 2, // signalCount
    32.  
    33. // signals: signature, parameters, type, tag, flags
    34. 19, 18, 18, 18, 0x05,
    35. 36, 32, 18, 18, 0x05,
    36.  
    37. // slots: signature, parameters, type, tag, flags
    38. 47, 18, 18, 18, 0x0a,
    39.  
    40. 0 // eod
    41. };
    42.  
    43. static const char qt_meta_stringdata_GrabberController[] = {
    44. "GrabberController\0\0onNewImage()\0fps\0"
    45. "onFps(int)\0updateFps()\0"
    46. };
    47.  
    48. const QMetaObject GrabberController::staticMetaObject = {
    49. { &QThread::staticMetaObject, qt_meta_stringdata_GrabberController,
    50. qt_meta_data_GrabberController, 0 }
    51. };
    52.  
    53. #ifdef Q_NO_DATA_RELOCATION
    54. const QMetaObject &GrabberController::getStaticMetaObject() { return staticMetaObject; }
    55. #endif //Q_NO_DATA_RELOCATION
    56.  
    57. const QMetaObject *GrabberController::metaObject() const
    58. {
    59. return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
    60. }
    61.  
    62. void *GrabberController::qt_metacast(const char *_clname)
    63. {
    64. if (!_clname) return 0;
    65. if (!strcmp(_clname, qt_meta_stringdata_GrabberController))
    66. return static_cast<void*>(const_cast< GrabberController*>(this));
    67. return QThread::qt_metacast(_clname);
    68. }
    69.  
    70. int GrabberController::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    71. {
    72. _id = QThread::qt_metacall(_c, _id, _a);
    73. if (_id < 0)
    74. return _id;
    75. if (_c == QMetaObject::InvokeMetaMethod) {
    76. switch (_id) {
    77. case 0: onNewImage(); break;
    78. case 1: onFps((*reinterpret_cast< int(*)>(_a[1]))); break;
    79. case 2: updateFps(); break;
    80. default: ;
    81. }
    82. _id -= 3;
    83. }
    84. return _id;
    85. }
    86.  
    87. // SIGNAL 0
    88. void GrabberController::onNewImage()
    89. {
    90. QMetaObject::activate(this, &staticMetaObject, 0, 0);
    91. }
    92.  
    93. // SIGNAL 1
    94. void GrabberController::onFps(int _t1)
    95. {
    96. void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)) };
    97. QMetaObject::activate(this, &staticMetaObject, 1, _a);
    98. }
    99. QT_END_MOC_NAMESPACE
    To copy to clipboard, switch view to plain text mode 

    Can anyone help me here?

    Cheers

    Thomas
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Devision By Zero Error while emitting signal

    From what I can see of your code, you are running a multi-threaded process with absolutely no locking or other synchronization on the memory you are using to store the grabbed data. You seem to be using the same _grabber instance in both threads.

    My guess is that the divide by zero error is just a symptom of the fact that your two threads are managing to completely corrupt your image data, because one of them continues to write to the _grabber->data() while the other is reading from it. Try wrapping the reading / writing calls in a mutex or other locking mechanism and see if that fixes the synchronization problem, or copy the data you have grabbed and pass that to the main GUI process instead of directly reading from your grab buffer.

  3. #3
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Devision By Zero Error while emitting signal

    Ok, sry, my bad. I forgot to explain, that in the first code piece _grabber is an instance of the actual Grabber-Class and _grabber in the second code is an instance of the threaded class GrabberController.

    So, there is just the GUI and this GrabberController-Thread. I checked for any synch problems but as far as I see ther are none.

    The GrabberController-Thread emits the signal onNewImage and does not continue and alter _data until the according slot newImage of the MainWindows has finished building and displaying the image. So there should be no problems at this point.

    Or am I mistaken?

    EDIT: tried copying the data before building the image but did not change a bit
    Last edited by thomers; 11th October 2011 at 09:19.

  4. #4
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Devision By Zero Error while emitting signal

    Well, since I cannot edit my previous post, I will have to reply to update the situation:

    I removed the connect-line in my MainWindow (connect(_grabber, SIGNAL(onNewImage()), SLOT(newImage()), Qt::QueuedConnection);), so that emitting the signal in the worker thread should end in void, but I still get an Division By Zero error, this time at the app.exec() line in my main() function. This puzzles me, since absolutely nothing is done with anything.
    But when I remove the emit onNewImage() line in the worker thread, I get no errors at all (but the data grabbing mechanism still works, proven by the FPS ...)

    So, still no idea whats going bananas here. Hoping for help.

    Cheers

    Thomas

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Devision By Zero Error while emitting signal

    Clean the project, re-run qmake, and rebuild the project. Does the problem persist?

  6. #6
    Join Date
    Oct 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Devision By Zero Error while emitting signal

    @ChrisW67: was actually the first thing I did and did it many many times again but no changes there.
    Also tried compiling on differen machines with different version of Qt but always this crappy error I can not find a reason for.

    It begins to look like I have to switch back to C# (where it works perfectly with exactly the same libraries etc.), but I would not like it ...

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Devision By Zero Error while emitting signal

    If you are convinced that your inter-thread communication is not corrupting the data, then I would check to be absolutely sure that the QImage you are creating is properly initialized, and has the proper parameters set. I am guessing that it might be possible to get a divide-by-zero fault if, for example, somewhere along the line some code internal to Qt was trying to calculate an aspect ratio for scaling the image, and the denominator turned out to be zero (or uninitialized garbage).

    I think you need to take a step back and try to isolate the source of the problem. For example, replace the part of your grabber thread that uses the external library with some emulation code that simply fills your grab buffer with some hard-coded stuff, and see if you still get the problem. Or set up a timer that emulates what is happening in your thread, and call the image creation slot every time it fires.

    If you're getting the same problem on every platform and with different versions of Qt, the finger is probably pointing at your code, not something wrong in Qt or your library.

Similar Threads

  1. emitting a signal with no button?
    By kja in forum Newbie
    Replies: 3
    Last Post: 29th November 2010, 21:33
  2. Problem emitting a signal
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 16th December 2009, 00:56
  3. Emitting signal from DLL to EXE
    By Miihkali in forum Qt Programming
    Replies: 6
    Last Post: 27th March 2009, 08:32
  4. Emitting signal causes CRASH
    By navi1084 in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2009, 16:17
  5. cost of emitting signal
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 29th November 2006, 08:53

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.