Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory access violation when appending to initialized QLinkedList
QtWS25 Last Chance

Memory access violation when appending to initialized QLinkedList

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    seanr8
    wrote on last edited by
    #1

    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) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    timer = new QTimer;
    sysFrame = new int;
    data = new QLinkedList<Data>;
    timer->start(500);
    connect(timer,SIGNAL(timeout(QPrivateSignal)),this,SLOT(handleTime()));
    *sysFrame =0;
    addr = new QString;
    msgs = new QString;
    lock = new QMutex;
    name = new QString;
    startDialog start(name,addr);
    start.setModal(true);
    start.exec();
    Server serv(sysFrame,data,lock,*addr,msgs);
    serv.StartServer();
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete sysFrame;
    delete timer;
    delete addr;
    delete data;
    delete msgs;
    delete lock;
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    Data tempData;
    QString tempString;
    tempString.clear();
    tempString.append("<").append(name).append(">: ");
    tempString.append(ui->lineEdit->text());
    ui->lineEdit->clear();
    lock->lock();
    tempData.frame=*sysFrame;
    tempData.text=tempString;
    data->append(tempData); //Memory access violation here.
    msgs->append(tempString);
    msgs->append("\n");
    lock->unlock();
    }

    void MainWindow::handleTime()
    {
    ui->textBrowser->clear();
    ui->textBrowser->setText(*msgs);
    ui->textBrowser->repaint();
    }
    @

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

    Any help is really appreciated,

    Thanks.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Achab
      wrote on last edited by
      #2

      Check the copy constructor of Data type.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        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.clear();
        tempString.append("<").append(name).append(">: ");@

        it should be
        @tempString.append("<").append(*name).append(">: ");@
        or
        @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,

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved