Results 1 to 9 of 9

Thread: QList and removeAt() question

  1. #1
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4

    Default QList and removeAt() question

    Hi; I have a little question about QList and memory usage. On my header I have a QList pointer:

    Qt Code:
    1. QList<MyObjectData> *myList;
    To copy to clipboard, switch view to plain text mode 

    on class an init method

    Qt Code:
    1. void MyClass::init(){
    2.  
    3. myList= new QList<MyObjectData>;
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    periodically I feed my list with new elements :

    Qt Code:
    1. void MyClass::feedList(int id){
    2.  
    3. MyObjectData mod;
    4. mod.setId(id);
    5. myList->append(mod);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    and method called by a timer take this data, use them and removing by calling removeAt(0) :


    Qt Code:
    1. void MyClass::useData(){
    2.  
    3. if(commandRequest->size()>0 && busy == false)
    4. {
    5. busy = true;
    6. executeSomething((MyObjectData )myList->at(0));
    7. myList->removeAt(0);
    8. busy = false;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    can be any memory issue whit these methods? thank you...

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QList and removeAt() question

    You don't show where you delete myList in your code. You should delete in init's counterpart (cleanup?) or your MyClass destructor so that you don't leak memory for the list itself. I would add the following code to do that:

    Qt Code:
    1. myList.clear(); // done by destructor, but doesn't hurt to clear IMHO
    2. delete myList;
    3. myList = nullptr;
    To copy to clipboard, switch view to plain text mode 

    Since you are not storing pointers to objects in your list, I don't see any issues with your handling of MyObjectData.

    P.S. You will crash if your myList is empty when you run this code, so also check that myList->size() > 0 in your if statement.
    Last edited by jefftee; 6th July 2015 at 17:04.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList and removeAt() question

    Quote Originally Posted by jefftee View Post
    Qt Code:
    1. myList.clear();
    2. delete myList; // some people prefer myList->deleteLater() but my pref is to control alloc/dealloc for object instances I manage.
    3. myList = NULL;
    To copy to clipboard, switch view to plain text mode 
    You don't need to clear() explicitly, the destructor does that.
    QList does not have a deleteLater() method, it is not a QObject subclass.
    The value of a null pointer is either 0 or nullptr

    Quote Originally Posted by jefftee View Post
    Since you are not storing pointers to objects in your list, I don't see any issues with your handling of MyObjectData.
    Indeed

    Quote Originally Posted by jefftee View Post
    P.S. You will crash if your myList is empty when you run this code, so also check that myList->size() > 0 in your if statement.
    And there is no need for the cast, QList::at() returns the correct type itself.

    Cheers,
    _

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QList and removeAt() question

    Quote Originally Posted by anda_skoa View Post
    You don't need to clear() explicitly, the destructor does that.
    QList does not have a deleteLater() method, it is not a QObject subclass.
    The value of a null pointer is either 0 or nullptr
    Thanks for correcting my post @anda_skoa. For that null pointer assignment, old habits are hard to break! I will update my post so as not to mislead anyone that runs across this in the future.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4

    Default Re: QList and removeAt() question

    thank you. In userData method there's already control of array lenght but I forget to rename QList when I post it on forum :

    Qt Code:
    1. //commandRequest is the real name of my QList, I call it "myList" only in this post
    2. if([B]commandRequest[/B]->size()>0 && busy == false)
    3. {
    4. busy = true;
    5. executeSomething((MyObjectData )myList->at(0));
    6. myList->removeAt(0);
    7. busy = false;
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QList and removeAt() question

    Ok, understood, but you'll get better responses when you provide actual code rather than dummy code for posting to forums. All too often people post code that won't compile or isn't actually the code they're using, or omit something they think isn't relevant, which can obscure the real problem, etc.

    BTW, what is your objective in using the busy boolean the way you show in the code?

    If your application isn't multi-threaded, then the busy boolean isn't needed because it's not protecting anything and if your code is actually multi-threaded and the QList is accessed by multiple threads concurrently, you should serialize access to the QList by locking/unlocking with a QMutex or use the QMutexLocker convenience class.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  7. #7
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4

    Default Re: QList and removeAt() question

    Ok thank you but I change only method's name. UseData method is periodically call by a QTimer so I decide to use boolen to prevent multiple launch when executeSomething(MyObjectData) method require more time than QTimer. Useless?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList and removeAt() question

    QTimer is event based.
    As long as your slot is executed, no further events can be processed.
    The timer can only fire again after execution has returned to the event loop.

    Cheers,
    _

  9. #9
    Join Date
    Mar 2014
    Posts
    6
    Qt products
    Qt4

    Default Re: QList and removeAt() question

    ah ok, thank you

Similar Threads

  1. QList question
    By DoomerDan in forum Qt Programming
    Replies: 8
    Last Post: 29th January 2015, 15:45
  2. QList Question
    By giblit in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2013, 01:41
  3. QList<QList > question
    By SubV in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 15:14
  4. QList question
    By aamer4yu in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2007, 13:43
  5. QList question
    By ln\ in forum Newbie
    Replies: 2
    Last Post: 19th December 2006, 13:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.