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 problem with QVector of QGraphicsLineItem objects

Memory problem with QVector of QGraphicsLineItem objects

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    QTDavid
    wrote on last edited by
    #1

    Hi,

    I am trying to have in my QGraphicsScene a number of lines that update whenever the window is resized, but my problem comes when I want to clear the update the QVector by clearing and appending the updated QGraphicsLineItem objects.
    Basically my problem is that whenever I try to clear the QVector, not all the memory allocated to it is released.

    Simplifying my code as far as I can gives me the following, but my issue persist:

    @
    // declare and initialise QVector
    QVector<QGraphicsLineItem *> *xGrid = new QVector<QGraphicsLineItem *>();

    // fill the QVector with a few QGraphicsLineItem objects
    for (int i = 0; i < 1000000; i++) {
    xGrid->append(new QGraphicsLineItem(10, 10, 10, 10));

    }
    

    qDebug() << xGrid->size(); // this prints 1000000

    // clear the QVector trying to free the allocated memory
    xGrid->clear();
    qDebug() << xGrid->size(); // this prints 0

    @

    Right after that for loop, my application is using about 354 Megabytes of memory (starting from 13.5 Mb).
    After the clear(), my application uses about 350 Megabytes of memory, so I guess something is clearly wrong of I am missing something here, as I would expect my memory usage to back to 13.5 Mb.

    Any advice would be helpful

    Thanks

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      QTDavid
      wrote on last edited by
      #2

      Ok I think i figured it out.
      I need to delete every QGraphicsLineItem object individually like so

      @
      for (int i = 0; i < xGrid->size(); i++) {
      delete xGrid->at(i);

      }
      xGrid->clear();@
      

      And then at the end I clear the QVector.

      By looking at the memory usage from Task Manager, for some reason I still feel like I need to do one last step. Can anyone confirm whether this is all I need to do?

      Thanks

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        You can also use

        @qDeleteAll(*xGrid);@

        To really get the memory usage down to your starting point you would have to call

        @xGrid->squeeze();@

        as well, after the clear(). (Though the actual use of this is rather limited in most cases)

        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