November 25, 2010

torsten torsten
Lab Rat
17 posts

Invalid parameter passed to C runtime function.

 

I have a problem with a application. I’ve created a debug handler with a own debug window. On a system without Qt, only the needed files, the windows works as expected, but if i work on a system installed with qt, the application crashs with the output “Invalid parameter passed to C runtime function.” and with an error box from Microsoft Visual C++ Runtime Library.

I’m not sure what the problem is. The debugwindowdemo is a simple ui class with some debug output. The other files i have posted here.

main.cpp

  1. #include "debugwindowdemo.h"
  2. #include "debughandler.h"
  3. #include <QtGui/QApplication>
  4. #include <QWidget>
  5. #include <QtGlobal>
  6. #include <QTextBrowser>
  7.  
  8.  
  9. int main( int argc, char* argv[] ) {
  10.     QApplication app( argc, argv );
  11.  
  12.     qInstallMsgHandler(debugWinMsgHandler);
  13.  
  14.     DebugWindowDemo *dialog = new DebugWindowDemo;
  15.     dialog->show();
  16.  
  17.     return app.exec();
  18. }

debughandler.h

  1. #ifndef DEBUGHANDLER_H
  2. #define DEBUGHANDLER_H
  3.  
  4. #include "debugwindow.h"
  5.  
  6. void debugWinMsgHandler (QtMsgType type, const char* msg) {
  7.     static DebugWindow* debugWindow = new DebugWindow;
  8.     debugWindow->move(0,0);
  9.     debugWindow->show();
  10.     switch(type) {
  11.     case QtDebugMsg:
  12.         debugWindow->append(QString(QObject::tr("D: %1")).arg(msg));
  13.         break;
  14.     case QtWarningMsg:
  15.         debugWindow->append(QString(QObject::tr("W: %1")).arg(msg));
  16.         break;
  17.     case QtCriticalMsg:
  18.         debugWindow->append(QString(QObject::tr("C: %1")).arg(msg));
  19.         break;
  20.     case QtFatalMsg:
  21.         debugWindow->append(QString(QObject::tr("F: %1")).arg(msg));
  22.         break;
  23.     }
  24. }
  25. #endif

debugwindow.h

  1. #ifndef DEBUGWINDOW_H
  2. #define DEBUGWINDOW_H
  3.  
  4. #include <QDialog>
  5.  
  6. namespace Ui {
  7.     class DebugWindow;
  8. }
  9.  
  10. class DebugWindow : public QDialog
  11. {
  12.     Q_OBJECT
  13.  
  14. public:
  15.     explicit DebugWindow(QWidget *parent = 0);
  16.     ~DebugWindow();
  17.  
  18. public slots:
  19.     void append(QString);
  20.  
  21. private:
  22.     Ui::DebugWindow *ui;
  23.  
  24. private slots:
  25.     void search(QString);
  26.     void print();
  27.     void save();
  28.  
  29. };
  30.  
  31. #endif // DEBUGWINDOW_H

debugwindow.cpp

  1. #include "debugwindow.h"
  2. #include "ui_debugwindow.h"
  3.  
  4. #include <QMessageBox>
  5. #include <QFileDialog>
  6. #include <QTextStream>
  7. #include <QPrintDialog>
  8. #include <QPrinter>
  9. #include <QFont>
  10.  
  11. DebugWindow::DebugWindow(QWidget *parent) :
  12.     QDialog(parent),
  13.     ui(new Ui::DebugWindow)
  14. {
  15.     QFont newFont("Ubuntu", 8);
  16.     this->setFont(newFont);
  17.     ui->setupUi(this);
  18.     ui->tbClear->setVisible(false);
  19.     connect(ui->leSearch,SIGNAL(textChanged(QString)),this,SLOT(search(QString)));
  20.     connect(ui->tbClear,SIGNAL(clicked()),ui->leSearch,SLOT(clear()));
  21.     connect(ui->pbPrint,SIGNAL(clicked()),this,SLOT(print()));
  22.     connect(ui->pbSave,SIGNAL(clicked()),this,SLOT(save()));
  23.     connect(ui->pbClose,SIGNAL(clicked()),this,SLOT(close()));
  24. }
  25.  
  26. DebugWindow::~DebugWindow()
  27. {
  28.     delete ui;
  29. }
  30.  
  31. void DebugWindow::append(QString str)
  32. {
  33.     ui->teLog->append(str);
  34. }
  35.  
  36. void DebugWindow::search(QString pattern)
  37. {
  38.     ui->tbClear->setVisible(!pattern.isEmpty());
  39.     QTextDocument* document = ui->teLog->document();
  40.  
  41.     QTextCharFormat noBackground;
  42.     noBackground.clearBackground();
  43.     QTextCharFormat highlight;
  44.     highlight.setBackground (QBrush(QColor(239,41,41,200)));
  45.  
  46.     QTextCursor allText  = QTextCursor(document);
  47.     allText.select(QTextCursor::Document);
  48.     allText.setCharFormat(noBackground);
  49.  
  50.     QRegExp expression = QRegExp(pattern,Qt::CaseInsensitive,QRegExp::RegExp2);
  51.  
  52.     QTextCursor cursor = document->find(expression,QTextCursor(document),0);
  53.     while (!cursor.isNull()) {
  54.         cursor.select(QTextCursor::LineUnderCursor);
  55.         cursor.setCharFormat(highlight);
  56.         cursor = document->find(expression,cursor,0);
  57.     }
  58. }
  59.  
  60. void DebugWindow::save()
  61. {
  62.     QString fileName = QFileDialog::getSaveFileName(this,QObject::tr("Logfile speichern"),QDir::homePath(),QObject::tr("Logfile (*.log)"));
  63.     if (!fileName.isEmpty())
  64.     {
  65.         if (!fileName.endsWith("log"))
  66.         {
  67.             fileName = fileName+".log";
  68.         }
  69.         QFile file(fileName);
  70.         if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
  71.         {
  72.             QString message = QString("%1%2%3")
  73.                               .arg(QObject::tr("Die Datei "))
  74.                               .arg(fileName)
  75.                               .arg(QObject::tr(" kann nicht gespeichert werden!"));
  76.             QMessageBox::critical(this,QObject::tr("Fehler beim Speichenrn der Logfile."),message);
  77.         }
  78.         else
  79.         {
  80.             QTextStream out(&file);
  81.             out.setGenerateByteOrderMark(true);
  82.             out << ui->teLog->toPlainText();
  83.             file.close();
  84.             QString message = QString("%1%2%3")
  85.                               .arg(QObject::tr("Die Logfile "))
  86.                               .arg(fileName)
  87.                               .arg(QObject::tr(" wurde erfolgreich gespeichert!"));
  88.             QMessageBox::information(this,QObject::tr("Logfile erfolgreich gespeichert!"),message);
  89.         }
  90.     }
  91. }
  92.  
  93. void DebugWindow::print()
  94. {
  95.     QTextDocument *document = ui->teLog->document();
  96.     QPrinter printer;
  97.     QPrintDialog printDialog(&printer);
  98.     if (printDialog.exec() != QDialog::Accepted)
  99.     {
  100.         return;
  101.     }
  102.     document->print(&printer);
  103. }

7 replies

November 25, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

What do you mean by “work on a system installed with qt”?
Which compilers do you use?
With which compiler was the installed Qt version build?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

November 25, 2010

torsten torsten
Lab Rat
17 posts

“work on a system installed with qt” means a installed qt sdk (including qt path in the $PATH)
I’m using the build 2010.05 mingw

November 26, 2010

Gerolf Gerolf
Hobby Entomologist
3251 posts

so, you are using build 2010.05 with mingw. And which version is installed on the machine, where it crashes? Are those versions the same? The error sounds (for a first read) to be caused by different versions or different builds (with different parameters, compilers etc). That’s why I’m asking…

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

November 26, 2010

torsten torsten
Lab Rat
17 posts

The same version is installed on the system where it crash. the “funny” part is, on a system without qt sdk, only with the needed qt files (libgcc_s_dw2-1.dll, mingwm10.dll, QtCored4.dll, QtGuid4.dll) it works.

Last night i found out (lot of things i’ve tried eg switching debugwindow from qdialog to qwidget), that it has to be a problem of the ui file (i think so)… i created a new designer class, copied!!! the code into the cpp/h files and “copied” the ui elements with the designer to the other ui file… and you wouldn’t believe it, it works.. i will post the original ui file… i haven’t found any errors within, but maybe there is another problem:

debugwindow.ui
http://pastebin.com/XpiyNF3K

November 26, 2010

torsten torsten
Lab Rat
17 posts

It seems to be an ansi/utf8 conversion error if the ui file is ansi encoded. i converted ui file to utf8. ive’d used the rc version of the qtcreater who was warning of wrong encoded file (so i figured it out).

February 22, 2011

snowpong snowpong
Lab Rat
51 posts

Thanks for the hint here Torsten – I had a similar problem here. The only difference was a .qml file was saved with whatever System encoding Mac runs, and on my Win7 I got the same “Invalid parameter passed to C runtime function.” error.

The fix was to “Save with Encoding” UTF-8 and rebuild – then it worked without errors? Did you file a bug-report?

_Update: _ Actually turned out to be another bug (exception thrown and the program didn’t know what to do) :) Nevermind, nothing to see here :D

February 22, 2011

torsten torsten
Lab Rat
17 posts

No, i didn’t file a bug, because it’s not a “bug” after all. Maybe they should add a feature to configure the basic encoding the creator should use. (I know, you can choose the encoding, but that doesn’t change the creator defaults).

 
  ‹‹ QXmlDefaultHandler fatalError\error\warning are not triggered during invalid XML doc parsing      Discussion regarding QWaitCondition. When to use? ››

You must log in to post a reply. Not a member yet? Register here!