Finding cause of corrupted shared library error (Q

2019-09-10 21:08发布

问题:

I have a fairly simple piece of code which launches a QProcess:

launchResultCode = ELaunchOk;
QDateTime beginTimeStamp = QDateTime::currentDateTime();
command->start(commandpath, myParameters);
if (command->waitForStarted(waitToStart)) {
    if (!myStdIn.isEmpty()) command->write(myStdIn.toLatin1());
    command->closeWriteChannel();
    qDebug() << "P1";
    if (command->waitForFinished(waitToFinish)) {
        myStdOut = command->readAllStandardOutput();
        myStdErr = command->readAllStandardError();
    } else {
        launchResultCode = ELaunchFinishFailed;
    }
} else {
    launchResultCode = ELaunchStartFailed;
}
qDebug() << "postcorrupt";

And it is causing a corrupted shared library error. When I run this code I get the output from gdb below. I'm trying to figure out what is at either memory location mentioned in the error, but there are no variables there! Can someone help me understand what is going wrong here?

(gdb) c
Continuing.
precorrupt
Detaching after fork from child process 21667.
P1
warning: Corrupted shared library list: 0x7fffe8008970 != 0x7ffff691b000
postcorrupt
[New Thread 0x7fffed453700 (LWP 21668)]

Breakpoint 1, RunProcessWorker::run (this=0x7fffffffcc30, whichMutex=RunProcessWorker::EMutexIP, activityID=..., commandFriendlyName=..., commandpath=..., 
    enableDebug=true, showDebugCommandLine=true, debugFilenameTemplate=..., myEnvironment=..., myParameters=..., myStdIn=..., myStdOut=..., myStdErr=..., 
    waitToStart=5000, waitToFinish=5000, actualRunTime=@0x7fffffffca58: 85, launchResultCode=@0x7fffffffca54: RunProcessWorker::ELaunchOk, 
    qprocessErrorCode=@0x7fffffffca50: QProcess::UnknownError, qprocessesExitCode=@0x7fffffffca6c: 0)
    at ../../src/external-sharedfiles/systemcommands/runprocessworker.cpp:292
292         command->deleteLater();
(gdb) info symbol 0x7fffe8008970
No symbol matches 0x7fffe8008970.
(gdb) info symbol 0x7ffff691b000
No symbol matches 0x7ffff691b000.
(gdb) 

Note that the error sometimes occurs before my P1 output, so it's something in that area but I can't figure out what! The process forked is a Qt library so I can't see into that library (and probably couldn't understand it)...does this mean it's a bug in Qt library?

Perhaps related, but valgrind shows memory lost on the QProcess start function:

30 (24 direct, 6 indirect) bytes in 1 blocks are definitely lost in loss record 837 of 2,936
  in RunProcessWorker::run(RunProcessWorker::EMutex, QString, QString, QString, bool, bool, QString, QStringList, QStringList, QString, QString&amp;, QString&amp;, unsigned int, unsigned int, unsigned long long&amp;, RunProcessWorker::ELaunchResultCodes&amp;, QProcess::ProcessError&amp;, int&amp;) in /mnt/lserver2/data/development/sharedfiles/systemcommands/runprocessworker.cpp:241
  1: operator new[](unsigned long) in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
  2: /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1
  3: /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1
  4: QProcess::start(QString const&amp;, QStringList const&amp;, QFlags&lt;QIODevice::OpenModeFlag&gt;) in /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1

回答1:

Note that your error message doesn't say "corrupted shared library" but rather "corrupted shared library list". That is, the list of shared libraries in your process's memory space is what is corrupted, rather than the shared libraries themselves. So my suspicion is not that you have a corrupted shared library, but rather that something is overwriting memory in your program's memory space and causing corruption that happens to damage that list.

Also it's interesting that your debugger specifies this as the site of the crash:

292         command->deleteLater();

As you may know, deleteLater() is a Qt method for causing a QObject to be deleted later (i.e. on the next iteration of the Qt event loop). The most likely reason why the program would crash here is that the (command) pointer that the method is being called on is invalid (either NULL or dangling). Is (command) in this case the same QProcess object as the one you are calling in your posted example code? If so, is it possible that you already deleted that QProcess object somewhere, leaving the crashing code above with a dangling pointer? (If you're not sure, you could subclass QProcess and put a qDebug() statement in your subclass's destructor, so that you could see in your stdout/stderr output just where and when the QProcess object was destroyed... and if that debug-print happens before the crash, then that's a good clue as to why the crash occurred).

Another possible problem would be if you are running the above code "stand-alone", without a QApplication (or QThread) object executing exec() in the same thread. Since deleteLater() posts a message to the Qt event loop, it won't work properly if there isn't a Qt event loop present and executing in the same thread.