Valgrind使用QApplication进行无效的读取和分段错误

时间:2016-03-31 12:06:16

标签: 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.

porting code Qt3 to Qt5 and from 32-bit to 64-bit.我的代码中的第73行是test = new char*[2];

我的程序似乎因showFullScreen()电话上的分段错误而崩溃。

变量test的使用是否可能导致Valgrind输出和程序崩溃? 64位Linux上的argc和argv的形式是否与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;
}

1 个答案:

答案 0 :(得分:0)

首先,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);