Results 1 to 19 of 19

Thread: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Wink Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    I'm "kicking the tires" on QtSerialPort.

    The Qt docs have this simple code fragment as an example of how to use SerialPortInfo:

    // Example use SerialPortInfo
    foreach (const SerialPortInfo &info, SerialPortInfo::availablePorts()) {
    qDebug() << "Name : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

    // Example use SerialPort
    SerialPort serial;
    serial.setPort(info);
    if (serial.open(QIODevice::ReadWrite))
    serial.close();
    }

    When I run the code, the loop runs once, and does the qDebug print. But, I get an exception when stepping out of the loop (at the curly brace).

    I commented out nearly everything, and still get the exception:

    int _tmain(int argc, _TCHAR* argv[])
    {
    QApplication app(argc, argv);
    app.setOrganizationName("Flying Nerd");

    app.setApplicationName("Stats Helpwer");

    // MainWindow mainWin;
    //mainWin.show();
    //mainWin.raise();

    // Example use SerialPortInfo
    foreach (const SerialPortInfo &info, SerialPortInfo::availablePorts()) {
    qDebug() << "Name : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

    // Example use SerialPort
    //SerialPort serial;
    //serial.setPort(info);
    //if (serial.open(QIODevice::ReadWrite))
    // serial.close();
    }

    return app.exec();

    }

    I haven't been able to build the docs for QtSerialPort class either. If I knew what SerialPortInfo::availablePorts returns (a container of some sort for SerialPortInfo &, I'm thinking), I'd try avoiding use of the foreach and just iterate through the items in the container.

    So, two questions:

    1) Is there class documentation online somewhere for the QtSerialPort classes (if not, I'll struggle more with building the docs).
    2) Anybody spot why I'm getting the exception?

    Thanks,

    Dave Thomas

  2. #2
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    @davethomaspilot

    1) Is there class documentation online somewhere for the QtSerialPort classes (if not, I'll struggle more with building the docs).
    No, the final documentation anywhere yet.
    On the WiKi written How To generate documentation.

    2) Anybody spot why I'm getting the exception?
    I do not know, use "enumerator" or "cenumerator" example from /examples .

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    what happens if you change to
    Qt Code:
    1. ...
    2. SequenceType sequence = SerialPortInfo::availablePorts();
    3.  
    4. foreach (const SerialPortInfo &info, sequence) {
    To copy to clipboard, switch view to plain text mode 
    Obviously you have to change SequenceType to whatever it is meant to be.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Quote Originally Posted by kuzulis View Post
    @davethomaspilot



    No, the final documentation anywhere yet.
    On the WiKi written How To generate documentation.



    I do not know, use "enumerator" or "cenumerator" example from /examples .

    Yes, I saw the How to generate documentation. But, it didn't work until I deleted a line in serialport.qdocconf which referenced $QT5. Once I got the class docs generated, I see that availablePorts() returns a QList<SerialPortInfo>.

    So, I avoided use of the foreach by using an iterator works:

    int _tmain(int argc, _TCHAR* argv[])
    {
    QApplication app(argc, argv);
    app.setOrganizationName("Flying Nerd");

    app.setApplicationName("Stats Helpwer");

    QList<SerialPortInfo> ports;
    ports = SerialPortInfo::availablePorts();

    QList<SerialPortInfo>::iterator i;
    for (i = ports.begin(); i != ports.end(); ++i)
    {
    qDebug() << "Name : " << i->portName();
    qDebug() << "Description : " << i->description();
    qDebug() << "Manufacturer: " << i->manufacturer();
    }

    So, I'm not sure what to conclude. I've been using Qt for a few months now, but I wasn't aware of the foreach construct until I saw the example for QtSerialPort. I can see using that all the time--much handier than declaring an iterator and remembering the syntax for looping through using the iterator.

    The build instructions on

    http://doc.qt.io/qt-4.8/http://qt-project.org/wiki/QtSerialPort

    don't quite match the source tree it references. So, while I got things to build, maybe I didn't do something quite right. The build instructions that are in the source tree are incomplete--they just say (literally) BLAH, BLAH, BLAH.

    Thanks,

    Dave Thomas


    Added after 4 minutes:


    Quote Originally Posted by amleto View Post
    what happens if you change to
    Qt Code:
    1. ...
    2. SequenceType sequence = SerialPortInfo::availablePorts();
    3.  
    4. foreach (const SerialPortInfo &info, sequence) {
    To copy to clipboard, switch view to plain text mode 
    Obviously you have to change SequenceType to whatever it is meant to be.
    That works!

    So what does it mean?

    Thanks,

    Dave Thomas
    Last edited by davethomaspilot; 23rd November 2012 at 22:56.

  5. #5
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Actually, not the assertion occurs when returning from main() instead of when exiting the foreach block.

    int _tmain(int argc, _TCHAR* argv[])
    {
    QApplication app(argc, argv);
    app.setOrganizationName("Flying Nerd");

    app.setApplicationName("Stats Helpwer");

    QList<SerialPortInfo> ports;
    ports = SerialPortInfo::availablePorts();


    MainWindow mainWin;
    mainWin.show();
    mainWin.raise();
    #if 1
    // Example use SerialPortInfo
    foreach (const SerialPortInfo &info,ports ) {
    qDebug() << "Name : " << info.portName();
    qDebug() << "Description : " << info.description();
    qDebug() << "Manufacturer: " << info.manufacturer();

    // Example use SerialPort
    SerialPort serial;
    serial.setPort(info);
    if (serial.open(QIODevice::ReadWrite))
    serial.close();
    }
    #endif
    return app.exec();

    }
    The assertion occurs in a delete in the SerialPort destructor.

    I'm going to try just creating then deleting a QList of SerialPortInfo

    Dave Thomas

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    means there is a bug in SerialPortInfo dtor...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    The destructor is empty:

    /*!
    Destroys the SerialPortInfo object. References to the values in the
    object become invalid.
    */
    SerialPortInfo::~SerialPortInfo()
    {
    }

    The code snippet with a SerialPortInfo is used in the console example also.

    Dave Thomas


    Added after 44 minutes:


    This is sufficient to cause the assertion to occur:


    QList<SerialPortInfo> ports;
    ports = SerialPortInfo::availablePorts();

    delete &ports;
    The assertion does NOT occur if I use the debug version of the SerialPort lib.

    So, I have my "workaround".
    Last edited by davethomaspilot; 24th November 2012 at 23:54.

  8. #8
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    So, in the end, how me reproduce the problem to try fix it?
    Give me brief step by step detailed information.

  9. #9
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Quote Originally Posted by kuzulis View Post
    So, in the end, how me reproduce the problem to try fix it?
    Give me brief step by step detailed information.
    Just create a Qlist of SeriaPortInfo then delete it:


    This is sufficient to cause the assertion to occur:


    QList<SerialPortInfo> ports;
    ports = SerialPortInfo::availablePorts();

    delete &ports;

    The assertion does NOT occur if I use the debug version of the SerialPort lib.
    Or, define the list as an local variable. When it goes out of scope, the assertion occurs.

    Dave Thomas

  10. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Quote Originally Posted by davethomaspilot View Post
    The destructor is empty:

    /*!
    Destroys the SerialPortInfo object. References to the values in the
    object become invalid.
    */
    SerialPortInfo::~SerialPortInfo()
    {
    }

    The code snippet with a SerialPortInfo is used in the console example also.

    Dave Thomas


    Added after 44 minutes:


    This is sufficient to cause the assertion to occur:




    The assertion does NOT occur if I use the debug version of the SerialPort lib.

    So, I have my "workaround".
    that is NOT valid c++!! you shouldn't 'delete' stack variables!

    Qt Code:
    1. void test()
    2. {
    3. {
    4. QList<SerialPortInfo> ports;
    5. ports = SerialPortInfo::availablePorts();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    that is better.

    are you mixing debug and release object code/libraries when testing this new class?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  11. #11
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    I'm can not reproduce problem:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3.  
    4. #include <serialportinfo.h>
    5.  
    6.  
    7. QT_USE_NAMESPACE_SERIALPORT
    8.  
    9. void test()
    10. {
    11. {
    12. QList<SerialPortInfo> ports;
    13. ports = SerialPortInfo::availablePorts();
    14.  
    15. foreach (const SerialPortInfo &info, ports)
    16. qDebug() << info.portName();
    17. }
    18. }
    19.  
    20. int main(int argc, char *argv[])
    21. {
    22. QCoreApplication a(argc, argv);
    23.  
    24. test();
    25.  
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    Qt 4.8.3 + MinGW4.4 -> release build

  12. #12
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    That causes the assertion for me.

    Qt 4.8.2 debug dlls, but SerialPort1.lib.

    When I use SerialPortd1.lib, the issue goes away.


    On the stack frame, there is a message 'scalar deleting destructor'().

    Let me know how I can help.

    Dave Thomas

  13. #13
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    right, so there is no problem then - just you using the wrong lib version
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  14. #14
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Shouldn't both the debug and non-debug version of the lib work?

  15. #15
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Are you serious? You cant mix up debug and non debug libs. So no, the release version shouldnt work if the rest of your app is debug version
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  16. #16
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    Yes, I was serious.

    Sorry for my stupidity. On *nix systems, we didn't have libs, and often mixed both debug and non-debug versions of dlls. I just assumed same would apply for windows.

    What a pain, to have to have both versions of ALL the binaries just to debug my relatively tiny application? But, another discussion, I know.

    Thanks!

    Dave Thomas

  17. #17
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    you shouldn't mix debug & release libs/dlls on *nix either.

    And you don't need release versions to debug your app...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  18. #18
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    you shouldn't mix debug & release libs/dlls on *nix either.
    Why not? One approach is to have the release's text section(code) to be identical so the only difference is the absence of debug data. This is a good approach where the binary size is important, but performance optimization is not worth the associated hassles.

    So,for libraries we always use non-debug dll's and always used binaries with debug data for the code we developed. We rarely needed to step into the library code but we needed the debug data until release time.

    Frequently, we wouldn't even turn on optimization for the release builds--just strip the debug data.

    "And you don't need release versions to debug your app.."

    You do if the debug versions won't fit in the space you have in your embedded application.

    Thanks,

    Dave Thomas
    Last edited by davethomaspilot; 3rd December 2012 at 14:49. Reason: Change is to isn't

  19. #19
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Block Type invalid Assertion when running SerialPortInfo in foreach snippet

    why? because you do not have a guarantee that the CRTs are compatible.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. invalid use of incomplete type 'struct MSG'
    By libed in forum Qt Programming
    Replies: 5
    Last Post: 21st February 2019, 12:32
  2. Why? Invalid type argument of unary *
    By progman in forum Newbie
    Replies: 4
    Last Post: 21st March 2011, 19:31
  3. QSocketNotifier: Invalid socket 12 and type 'Read', disabling...
    By kunalnandi in forum General Programming
    Replies: 1
    Last Post: 24th September 2008, 18:09
  4. Invalid return type
    By Salazaar in forum Newbie
    Replies: 3
    Last Post: 5th June 2007, 17:51
  5. invalid use of undefined type
    By mikro in forum Newbie
    Replies: 2
    Last Post: 9th April 2006, 12:10

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.