Results 1 to 8 of 8

Thread: Invalid parameter passed to C runtime function

  1. #1
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Invalid parameter passed to C runtime function

    Hi everybody,

    have some trouble with Runtime library on step run of application (compiling was successful, build too) but when I press Start have a crash with this tags:

    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.
    terminate called after throwing an instance of ‘std::system_error’ what(): Invalid argument
    QObject::killTimers: timers cannot be stopped from another thread
    Use: Qt 5.3 win MinGW 4.8 ×32 but run on x64 system, maybe problem here but i’m not sure
    or because I try to combine c++11 code with Qt, but not sure too, because Qt traslate all it code to C++…
    cannibals.h
    Qt Code:
    1. #ifndef CANNIBALS_H
    2. #define CANNIBALS_H
    3. #include <thread>
    4. #include <mutex>
    5. #include <time.h>
    6. #include <windows.h>
    7. #define M 100
    8.  
    9. #include <QObject>
    10.  
    11. class Cannibals : public QObject
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit Cannibals(QObject *parent = 0);
    16. void Dinner_a(int);
    17.  
    18. private:
    19. void Cooking();
    20. void Dinner();
    21. std::thread Cook;
    22. std::mutex eating;
    23. int food=10;
    24.  
    25. signals:
    26. void NumberChanged(int);
    27.  
    28. public slots:
    29.  
    30. };
    31.  
    32. #endif // CANNIBALS_H
    To copy to clipboard, switch view to plain text mode 
    cannibals.cpp
    Qt Code:
    1. #include "cannibals.h"
    2.  
    3. Cannibals::Cannibals(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    7.  
    8. void Cannibals::Cooking()
    9. {
    10. food=M;
    11. }
    12.  
    13. void Cannibals::Dinner_a(int z)
    14. {
    15. eating.lock();
    16. for(int i=0; i<z;i++){
    17. Dinner();}
    18. eating.unlock();
    19. }
    20.  
    21. void Cannibals::Dinner()
    22. {
    23. if(food!=0){
    24. emit NumberChanged(food);
    25. --food;}
    26. else
    27. Cook = std::thread(&Cannibals::Cooking, this);
    28. Cook.join();
    29.  
    30. Sleep(rand() 00);
    31. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3. #include "cannibals.h"
    4. #include <ui_mainwindow.h>
    5. #include <QMainWindow>
    6. #include <thread>
    7. #define N 20
    8. #define M 100
    9. #define P (M/N)
    10.  
    11. namespace Ui {
    12. class MainWindow;}
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21. Cannibals *Foo;
    22. std::thread Cannibal[N];
    23.  
    24. private slots:
    25. void on_pushButton_clicked();
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29.  
    30. public slots:
    31. void OnNumberChanged (int);
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. Foo = new Cannibals();
    10. connect(Foo,SIGNAL(NumberChanged(int)),this,SLOT(OnNumberChanged(int)));
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::on_pushButton_clicked()
    19. {
    20.  
    21. for(int i=0;i<N;i++)
    22. {
    23. Cannibal[i] = std::thread(&Cannibals::Dinner_a, Foo, P);
    24. }
    25.  
    26. for(int i=0;i<N;i++)
    27. {
    28. Cannibal[i].join();
    29. }
    30. }
    31.  
    32. void MainWindow::OnNumberChanged(int Num)
    33. {
    34. ui->listWidget->clear();
    35. ui->pushButton->setEnabled(false);
    36. ui->listWidget->addItem(QString(Num) + " cannibal ate");
    37. }
    To copy to clipboard, switch view to plain text mode 

  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: Invalid parameter passed to C runtime function

    Is this some kind of academic exercise or does the example have any practical goal? What is the point of all those threads?
    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
    Nov 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Invalid parameter passed to C runtime function

    Quote Originally Posted by wysota View Post
    Is this some kind of academic exercise or does the example have any practical goal? What is the point of all those threads?
    Yes, this is exercise to practice in multithreading, but GUI is required condition and I chose Qt. About point: 20 threads - children and 1 thread - mother, children eat plumps, and when bowl is empty call mom to fill it. Amount of children and plumps is variables...

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Invalid parameter passed to C runtime function

    The Qt signal/slot code might not recognize the non-Qt thread correctly and chose to handle your connect() as a DirectConnection instead of QueuedConnection.

    Try it with explicitly specifying that
    Qt Code:
    1. connect(Foo,SIGNAL(NumberChanged(int)),this,SLOT(OnNumberChanged(int)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Nov 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Post Re: Invalid parameter passed to C runtime function

    CanCan.zip
    Quote Originally Posted by anda_skoa View Post
    Qt Code:
    1. connect(Foo,SIGNAL(NumberChanged(int)),this,SLOT(OnNumberChanged(int)), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    This is have no effect, add full project for tests.

  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: Invalid parameter passed to C runtime function

    Quote Originally Posted by Satansoft View Post
    Yes, this is exercise to practice in multithreading, but GUI is required condition and I chose Qt. About point: 20 threads - children and 1 thread - mother, children eat plumps, and when bowl is empty call mom to fill it. Amount of children and plumps is variables...
    I think you overcomplicated the code. And shouldn't the producer be a thread living all the time just waiting for the consumers to tell it to start producing? Right now you start a thread only to set a variable and then the thread dies. With the architecture you have there is completely no benefit from having threads so I doubt this is what your professor had in mind.
    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
    Nov 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Invalid parameter passed to C runtime function

    Quote Originally Posted by wysota View Post
    I think you overcomplicated the code. And shouldn't the producer be a thread living all the time just waiting for the consumers to tell it to start producing? Right now you start a thread only to set a variable and then the thread dies. With the architecture you have there is completely no benefit from having threads so I doubt this is what your professor had in mind.
    Perhaps and overcomplicated, but task must be solved. I chose C++11 methods by reason of transparency, have any ideas to solve it?

  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: Invalid parameter passed to C runtime function

    Quote Originally Posted by Satansoft View Post
    Perhaps and overcomplicated, but task must be solved. I chose C++11 methods by reason of transparency, have any ideas to solve it?
    If it is your school task then we cannot interfere with it, we can only give you hints regarding the use of Qt. I can only suggest to divide your code into classes -- one for the producer, one for the consumer and one for management and user-interface. Then it should be much easier for you to comprehend what is where. If using std::thread is not an explicit requirement then you might want to switch to using QThread.
    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. The following user says thank you to wysota for this useful post:

    Satansoft (29th November 2014)

Similar Threads

  1. Replies: 6
    Last Post: 25th January 2014, 10:12
  2. Replies: 1
    Last Post: 9th January 2014, 15:31
  3. Replies: 4
    Last Post: 4th January 2012, 22:43
  4. Replies: 0
    Last Post: 24th October 2010, 19:09
  5. Replies: 1
    Last Post: 25th September 2010, 08:20

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.