Memory access violation when appending to initialized QLinkedList

  • Hey,

    So I've written a MainWindow that starts a few things, one of which is a server that handles connections in another thread. It's basically a peer to peer instant message client over TCP. I haven't really been able to test that it's working correctly because I'm getting a memory access violation when appending text to my QLinkedList of data items. The underlying threads have access to this list and on a timer scan the list to find the data that still needs to be sent over that socket. In the main window, when the user clicks the pushbutton the text that is in the box should be appended into that list of data to be sent by the sockets. However, every time I attempt to append something, I get a memory access violation error. I've been trying to debug this for hours stepping through with Visual Studio and I am now looking for a fresh set of eyes.

    Here's my MainWindow class implementation:

    @MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)
    timer = new QTimer;
    sysFrame = new int;
    data = new QLinkedList<Data>;
    *sysFrame =0;
    addr = new QString;
    msgs = new QString;
    lock = new QMutex;
    name = new QString;
    startDialog start(name,addr);
    Server serv(sysFrame,data,lock,*addr,msgs);

    delete sysFrame;
    delete timer;
    delete addr;
    delete data;
    delete msgs;
    delete lock;
    delete ui;

    void MainWindow::on_pushButton_clicked()
    Data tempData;
    QString tempString;
    tempString.append("<").append(name).append(">: ");
    data->append(tempData); //Memory access violation here.

    void MainWindow::handleTime()

    The line that fails on me is commented. It's in the "on_pushbutton_clicked" method.

    Any help is really appreciated,


  • Check the copy constructor of Data type.

  • Sorry, I don't know the real problem of your codes, maybe just like Achab said, your <Data> type has some design flaw.

    The codes may cause problem :

    1 : manually allocate and deallocate memory
    @addr = new QString;
    msgs = new QString;
    lock = new QMutex;

    delete sysFrame;
    delete timer;
    delete addr;
    delete data;
    delete msgs;
    delete lock;
    delete ui;@

    They work fine if no exceptions are thrown, if it did happens, the destructor wouldn't be called because the object haven't fully initialize can't call destructor, so your memory will leak.

    simple solution 1 -- allocate the object on stack

    @QString addr;
    QString name;@

    simple solution 2 -- use smart pointer
    @std::unique<QString> addr(new QString);
    std::unique<QString> name(new QString);@

    The rule of thumb is--always obey the rules of RAII

    2 : append "pointer" rather than QString "value"
    @ QString tempString;
    tempString.append("<").append(name).append(">: ");@

    it should be
    @tempString.append("<").append(*name).append(">: ");@
    @tempString.append("<" + *name + ">:");@

    rarely see programmers allocate QString or QLinkedList on heap, we usually put them on stack, these objects will take care of their own resource automatically.Even you don't write your own copy constructor and copy assignment, those object allocated on stack(QString, QLinkedList, std::list, std::vector, QList and other's) will handle the copy behavior properly, but that kind of heavy copy may not the behavior you like,

Log in to reply