ARM glibc中的分段错误

时间:2014-10-22 07:26:19

标签: c++ linux qt arm glibc

当以下代码的4个或更多进程在i.MX6自定义板上运行时, glibc 2.17库中发生了分段错误。

#include <execinfo.h>
#include <signal.h>

#include <QtCore/QCoreApplication>
#include <QtCore/QString>
#include <QtCore/QTimer>
#include <QtCore/QFile>
#include <QtCore/QByteArray>
#include <QtCore/QHash>

class DebugClassMap : public QHash<int, QString*>
{
public:
    DebugClassMap()
    {
        for(int i = 0; i < 7000; ++i)
        {
            insert(i, new QString("AAAA"));
        }
    }

    virtual ~DebugClassMap()
    {
        clear();
    }

    void clear()
    {
        for (iterator it = begin(); it != end(); ++it)
        {
             delete it.value();
             it.value() = 0;
        }
        QHash::clear();
    }

    DebugClassMap& operator=(const DebugClassMap& aMap)
    {
        clear();
        for (const_iterator it = aMap.begin(); it != aMap.end(); ++it)
        {
            insert(it.key(), new QString(*(it.value()))/*->clone()*/);
        }
        return *this;
    }
};

int main(int argc, char *argv[])
{
    // Application (MUST BE FIRST IN ORDER TO SETUP QT THREAD REGISTERING!)
    QCoreApplication app(argc, argv);

    DebugClassMap dbgMap1;
    DebugClassMap dbgMap2;

    while(true)
    {
        dbgMap1 = dbgMap2;
    }

    app.exec();

    return 0;
}

以下是上述代码的回溯。 看起来很奇怪__libc_malloc()需要在#3中分配524个字节。 但是_int_malloc()需要在#2中分配非常大的内存(722898944字节)。

722898944为0x2b169000为十六进制,与av(0x2b1694d4)的值非常相似。 所以我认为这个问题的原因在于glibc库。

请告诉我如何解决它。

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 8297]
malloc_consolidate (av=0x2b1694d4) at malloc.c:4073
4073                unlink(p, bck, fwd);
(gdb) bt
#0  malloc_consolidate (av=0x2b1694d4) at malloc.c:4073
#1  malloc_consolidate (av=0x2b1694d4) at malloc.c:4019
#2  0x2b0a7dd0 in _int_malloc (av=0x2b1694d4, bytes=722898944) at malloc.c:3358
#3  0x2b0aa234 in __libc_malloc (bytes=524) at malloc.c:2859
#4  0x2af55364 in operator new(unsigned int) ()
   from /target//lib/libstdc++.so.6
#5  0x2af5541c in operator new[](unsigned int) ()
   from /target//lib/libstdc++.so.6
#6  0x2acabb24 in QHashData::rehash(int) ()
   from /target/export/QtArm/qt-arm-4.8.2-10sep2013/lib/libQtCore.so.4
#7  0x00008df0 in QHashData::willGrow (this=0x15268)
    at /export/QtArm/qt-arm-4.8.2-10sep2013/include/QtCore/qhash.h:163
#8  0x00009340 in QHash<int, QString*>::insert (this=0x7eb07af4,
    akey=@0x8b36c, avalue=@0x7eb07ac4)
    at /export/QtArm/qt-arm-4.8.2-10sep2013/include/QtCore/qhash.h:761
#9  0x00009194 in DebugClassMap::operator= (this=0x7eb07af0, aMap=...)
    at main.cpp:87
#10 0x00008aec in main (argc=1, argv=0x7eb07c54) at main.cpp:103

0 个答案:

没有答案