[SOLVED]SIGSEGV QArrayData::deallocate(QArrayData*, unsigned int, unsigned int)



  • @QByteArray buffer; //declared as public in the header file

    void MyClass::run(){

    while(!bQuit){
        mutex.lock();
        buffer = readData(); // On this line i get the SIGSEGV
        mutex.unlock();
    }
    

    }@

    the method readData() return a QByteArray

    the backtrace is:
    @>&"bt\n"

    ~"#0 0xb7fdd424 in __kernel_vsyscall ()\n"
    ~"#1 0xb6c011df in __GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64\n"
    ~"#2 0xb6c04825 in __GI_abort () at abort.c:91\n"
    ~"#3 0xb6c3e39a in __libc_message (do_abort=2, fmt=0xb6d39888 "*** glibc detected *** %s: %s: 0x%s **\n") at ../sysdeps/unix/sysv/linux/libc_fatal.c:201\n"
    ~"#4 0xb6c48ee2 in malloc_printerr (action=<optimized out>, str=<optimized out>, ptr=0xaeb5a2a8) at malloc.c:5039\n"
    ~"#5 0xb6f25eb5 in QArrayData::deallocate(QArrayData
    , unsigned int, unsigned int) () from /home/tizio/Qt5.1.1/5.1.1/gcc/lib/libQt5Core.so.5\n"
    ~"#6 0xb6f27cd0 in QByteArray::operator=(QByteArray const&) () from /home/tizio/Qt5.1.1/5.1.1/gcc/lib/libQt5Core.so.5\n"
    ~"#7 0x0806a68f in MyClass::run (this=0x81a20c0) at ../Project/myclass.cpp:60\n"
    ~"#8 0xb6f253a9 in ?? () from /home/tizio/Qt5.1.1/5.1.1/gcc/lib/libQt5Core.so.5\n"
    ~"#9 0xb6856d4c in start_thread (arg=0xafcfdb40) at pthread_create.c:308\n"
    ~"#10 0xb6cc2bae in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130\n"
    197^done@

    This error don't appears always, only sometimes


  • Moderators

    Hi,

    What does MyClass inherit?

    How did you implement readData()?

    How are you using your threads?

    I recommend reading "this page":http://doc-snapshot.qt-project.org/qt5-stable/threads-synchronizing.html for info about transferring data between threads. Also see "this page":http://doc-snapshot.qt-project.org/qt5-stable/threads-technologies.html for recommendations how to use threads.



  • 1 - MyClass inherits QThread.

    2 - i have a class Utility with the public method readData(), for this class i use the extern keyword : "extern Utility mUtility;"
    in the read data method i write the following code:

    @int iPlcConnResult = 0;

    QByteArray buff;
    buff.clear();

    unsigned char data = (unsigned char)malloc(length);

    //mutexPlcSocket.lock();
    iPlcConnResult = readBytes(conn, tipoArea, numDB, startAddress, length, data);
    //mutexPlcSocket.unlock();

    bPlcConnError = (iPlcConnResult != 0);
    if (!bPlcConnError)
    buff = buff.setRawData((char*) data, length);
    else
    sPlcConnError = "Error " + QString::number(iPlcConnResult) + " - " + daveStrerror(iPlcConnResult);

    return buff;@

    3 - i start the thread with myThread.start(QThread::HighestPriority);


  • Moderators

    Hi,

    There isn't enough code to know for sure what's the cause, but you really shouldn't be using malloc in C++. There are safer ways to allocate memory.

    Using C-style casts can be dangerous too. Use static_cast<> instead. http://stackoverflow.com/questions/103512/in-c-why-use-static-castintx-instead-of-intx

    Questions:

    • Where did you call free?
    • Is Utility thread-safe?

    Try this, instead of calling malloc and QByteArray::setRawData():
    @
    // Create an heap-allocated array, filled with null bytes
    QByteArray buff(length, '\0');

    iPlcConnResult = readBytes(conn, tipoArea, numDB, startAddress,
    length, static_cast<unsigned char*>(buff.data()));
    @



  • I have been through with this same problem too. Thanks for the help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.