c++ - QApplication 的 Valgrind 无效读取和段错误

标签 c++ linux qt valgrind

我的 Qt 应用程序遇到运行时错误。这是我的 Valgrind 输出的一部分。之前还有很多,但要发布的内容很多。

==13659== Invalid read of size 8
==13659==    at 0x75E4085: QCoreApplication::arguments() (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x1B4E0114: ??? (in /usr/local/Qt/5.2.1/gcc_64/plugins/platforms/libqxcb.so)
==13659==    by 0x1B4E10E8: ??? (in /usr/local/Qt/5.2.1/gcc_64/plugins/platforms/libqxcb.so)
==13659==    by 0x1B9C0D2E: _SmcProcessMessage (in /usr/lib/x86_64-linux-gnu/libSM.so.6.0.1)
==13659==    by 0x1BBD48A5: IceProcessMessages (in /usr/lib/x86_64-linux-gnu/libICE.so.6.3.0)
==13659==    by 0x76096B0: QMetaObject::activate(QObject*, int, int, void**) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x767B1FD: QSocketNotifier::activated(int, QSocketNotifier::QPrivateSignal) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x7616840: QSocketNotifier::event(QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x65D20F3: QApplicationPrivate::notify_helper(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Widgets.so.5.2.1)
==13659==    by 0x65D56AD: QApplication::notify(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Widgets.so.5.2.1)
==13659==    by 0x75E0733: QCoreApplication::notifyInternal(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x7630A55: ??? (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==  Address 0x178ddfb0 is 0 bytes after a block of size 16 alloc'd
==13659==    at 0x4C28147: operator new[](unsigned long) (vg_replace_malloc.c:348)
==13659==    by 0x4FAD41: GUIApp::init() (GUIApp.cpp:73)
==13659==    by 0x423009: main (main.cpp:121)
==13659== 
==13659== Invalid read of size 1
==13659==    at 0x4C294E2: strlen (mc_replace_strmem.c:390)
==13659==    by 0x75E409B: QCoreApplication::arguments() (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x1B4E0114: ??? (in /usr/local/Qt/5.2.1/gcc_64/plugins/platforms/libqxcb.so)
==13659==    by 0x1B4E10E8: ??? (in /usr/local/Qt/5.2.1/gcc_64/plugins/platforms/libqxcb.so)
==13659==    by 0x1B9C0D2E: _SmcProcessMessage (in /usr/lib/x86_64-linux-gnu/libSM.so.6.0.1)
==13659==    by 0x1BBD48A5: IceProcessMessages (in /usr/lib/x86_64-linux-gnu/libICE.so.6.3.0)
==13659==    by 0x76096B0: QMetaObject::activate(QObject*, int, int, void**) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x767B1FD: QSocketNotifier::activated(int, QSocketNotifier::QPrivateSignal) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x7616840: QSocketNotifier::event(QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x65D20F3: QApplicationPrivate::notify_helper(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Widgets.so.5.2.1)
==13659==    by 0x65D56AD: QApplication::notify(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Widgets.so.5.2.1)
==13659==    by 0x75E0733: QCoreApplication::notifyInternal(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==  Address 0x50 is not stack'd, malloc'd or (recently) free'd
==13659== 
==13659== 
==13659== Process terminating with default action of signal 11 (SIGSEGV)
==13659==  Access not within mapped region at address 0x50
==13659==    at 0x4C294E2: strlen (mc_replace_strmem.c:390)
==13659==    by 0x75E409B: QCoreApplication::arguments() (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x1B4E0114: ??? (in /usr/local/Qt/5.2.1/gcc_64/plugins/platforms/libqxcb.so)
==13659==    by 0x1B4E10E8: ??? (in /usr/local/Qt/5.2.1/gcc_64/plugins/platforms/libqxcb.so)
==13659==    by 0x1B9C0D2E: _SmcProcessMessage (in /usr/lib/x86_64-linux-gnu/libSM.so.6.0.1)
==13659==    by 0x1BBD48A5: IceProcessMessages (in /usr/lib/x86_64-linux-gnu/libICE.so.6.3.0)
==13659==    by 0x76096B0: QMetaObject::activate(QObject*, int, int, void**) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x767B1FD: QSocketNotifier::activated(int, QSocketNotifier::QPrivateSignal) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x7616840: QSocketNotifier::event(QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==    by 0x65D20F3: QApplicationPrivate::notify_helper(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Widgets.so.5.2.1)
==13659==    by 0x65D56AD: QApplication::notify(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Widgets.so.5.2.1)
==13659==    by 0x75E0733: QCoreApplication::notifyInternal(QObject*, QEvent*) (in /usr/local/Qt/5.2.1/gcc_64/lib/libQt5Core.so.5.2.1)
==13659==  If you believe this happened as a result of a stack
==13659==  overflow in your program's main thread (unlikely but
==13659==  possible), you can try to increase the size of the
==13659==  main thread stack using the --main-stacksize= flag.
==13659==  The main thread stack size used in this run was 8388608.

我正在将代码 Qt3 移植到 Qt5 以及从 32 位移植到 64 位。 我下面代码中的第 73 行是 test = new char*[2]; .

我的程序似乎因 showFullScreen() 调用中的段错误而崩溃。

变量 test 的使用会不会是 Valgrind 输出和程序崩溃的原因? argc 和 argv 在 64 位 Linux 上的形式与 32 位相同吗?

void GUIApp::init()
{
    QApplication::setStyle("motif");

    int nbrparams=1;
    test = new char*[2];
    test[0] = new char[100];
    test[1] = new char[100];
        printf("  Test : %d\n", (int)sizeof test );     //   Test : 8
        printf(" *Test : %d\n", (int)sizeof *test );    //  *Test : 8
        printf("**Test : %d\n", (int)sizeof **test );   // **Test : 1
    strcpy(test[0], "gv_GUI");

    go_app = new QApplication( nbrparams,test );

    translator = new QTranslator( 0 );
    // load translation file, make sure that this symbolic link points to the desired translation file
    if (translator->load("tt2_go.qm")) {
        go_app->installTranslator( translator );
    }

    gaw = new GO_QT_Application_Widget( GO_GUI_MODE, mytalkbackptr, NULL, "QTApp");
    gaw->init();
    gaw->startup();

    gaw->move(0,0);
    gaw->setFixedSize(3200,1200);
    gaw->showFullScreen();

    init_done = true;
}

最佳答案

首先,argv 数组应该在最后一个有效参数之后有一个 NULL 指针,因此 argv[argc] 始终为 NULL。因此,应该修复您的代码的最小调整是

test[1] = NULL;    /* instead of test[1] = new char[100]; */

其次,我不明白对参数进行 new 的意义,因为您总是希望它们有效。也许它有一些我看不到的优势(我有 C 背景,你知道),但下面是我会为此编写的代码。行得通。

char * progName = "myProg";
char * arg1 = "-n";
char * arg2 = "10";

char *test[] = {
    progName,
    arg1,
    arg2,
    NULL
};

int nbrparams=3;

go_app = new QApplication(nbrparams, test);

关于c++ - QApplication 的 Valgrind 无效读取和段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36332994/

相关文章:

c++ - 在 Linux Mint QT 上构建 C++ Allegro 5 失败

linux - RedHat yum 颠覆安装

c++ - QSql模块与多线程应用

python - 具有动态宽度列的 QML TableView

c++ - 存储部分和的二叉树 : Name and existing implementations

c++ - 如何使用 std::sort 按特定顺序对字符串进行排序

c++ - 如何在 C++ 中进行文件检查

linux - 在同一个 shell 中执行一个 block

c++ - 尽管 QSqlQuery::exec 失败,但未提供错误描述

C++ 泛型函数的灵活性