Results 1 to 18 of 18

Thread: Deleting a thread content

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Deleting a thread content

    Hi! Is there any way to delete the content of a thread which is terminated using the terminate method? I tried to put a delete of the allocated variables of the thread after the terminate, even with a wait() in between, but the content (images for instance) are not deallocated. Any way to do this?
    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    What do you mean by "content of the thread"?

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Deleting a thread content

    The only way to do this is to ask the thread to terminate gracefully (and thus cleanup after itself) by sending it a kill signal rather than just killing it.

  4. #4
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Deleting a thread content

    Inside I have some pointers like QString* and QImage*, and I use the new operator to dynamically create objects. I need to delete these, but the delete only works when the thread exits from the run() function. If I terminate it, the delete instructions I placed in the GUI thread (not in the terminated thread) simply does nothing. This is what I placed in my GUI thread:

    Qt Code:
    1. newThread->terminate();
    2. delete newThread->pointer1;
    3. delete newThread->pointer2;
    4. ...
    To copy to clipboard, switch view to plain text mode 

    I thought this could work but I was wrong. What has been created is not deleted. Any way to do this? I found this message but I don't know if it can be useful in this case: http://lists.trolltech.com/qt-intere...ad00221-0.html.
    Terminate gracefully means I should use the quit() method instead of terminate? How does it cleans up itself?
    Many many thanks guys!

    EDIT: Ok, maybe I was wrong... The problem is also related to a read() method of a QImageReader. It is local to the run() method, but I suppose, as you said, it remains there and can't be deleted unless I gracefully quit the thread. No other way to delete what remains after the termination?
    Last edited by Luc4; 19th April 2010 at 13:53.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    QStrings and QImages shouldn't be kept as pointers at all. Change them to regular objects and all will be fine. And you should never call QThread::terminate(). That's just a failsafe if you have to kill a thread and don't care about anything else.

  6. #6
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Deleting a thread content

    OK, but I suppose we would be back to the problem you helped me with in the thread http://www.qtcentre.org/threads/2968...ory?highlight=. If I terminate the thread, it seems I can stop the read method, otherwise my thread finishes the loading. I tried using the quit() method, and in that case everything is right, like you said. Unfortunately, I have to wait many seconds till the image is completely loaded.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    You can't eat the cake and still have the cake. QImage was just not meant to be interrupted while loading. You can use lower level calls by wrapping libjpeg/libpng and others yourself instead of going through QImage.

  8. The following user says thank you to wysota for this useful post:

    Luc4 (20th April 2010)

  9. #8
    Join Date
    Apr 2010
    Location
    Switzerland
    Posts
    16
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Deleting a thread content

    Quote Originally Posted by wysota View Post
    QStrings and QImages shouldn't be kept as pointers at all.
    why not?
    do you mean inside thread.. or in general?

  10. #9
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Deleting a thread content

    I was about to ask the same as well. Moreover, I can't find anything about deleting a thread, except for that link I reported before. If I create a thread by using the new operator, do I have to delete that thread after it has finished using the delete operator? Is it possible to use delete this as the last instruction of the run() method?
    Thanks!

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    Quote Originally Posted by Nik8768 View Post
    why not?
    Because there are no benefits of keeping pointers as both these classes are implicitly shared and value-based.
    do you mean inside thread.. or in general?
    In general.

  12. The following user says thank you to wysota for this useful post:

    Nik8768 (20th April 2010)

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    Quote Originally Posted by Luc4 View Post
    I was about to ask the same as well. Moreover, I can't find anything about deleting a thread, except for that link I reported before. If I create a thread by using the new operator, do I have to delete that thread after it has finished using the delete operator? Is it possible to use delete this as the last instruction of the run() method?
    Thanks!
    QThread object is not a thread. It's just an object that let's you start a thread. Only the contents of the run() method are ran in the worker thread. So it's enough to return from run() and the thread will end. Of course you still have to do something with the QThread object, but there is nothing special about it - it's a QObject like any other.

  14. #12
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Deleting a thread content

    Quote Originally Posted by wysota View Post
    QThread object is not a thread. It's just an object that let's you start a thread. Only the contents of the run() method are ran in the worker thread. So it's enough to return from run() and the thread will end. Of course you still have to do something with the QThread object, but there is nothing special about it - it's a QObject like any other.
    So good solution is to make local variables in the run() method (for example instead of member variables if it is possible) so they are created in new thread and deleted when this thread ends (it ends when run() ends). So such local thread data is self cleaned on thread finish. Tadam! :]

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    Quote Originally Posted by faldżip View Post
    So good solution is to make local variables in the run() method (for example instead of member variables if it is possible) so they are created in new thread and deleted when this thread ends (it ends when run() ends). So such local thread data is self cleaned on thread finish. Tadam! :]
    Yes, exactly. And if you need to expose signals and slots outside you can do it like so:
    Qt Code:
    1. class Thread : public QThread {
    2. public:
    3. const Object *myInterface() const { return m_iface; }
    4. void run() {
    5. Object o; // lives in the worker thread, declares signals and slots
    6. m_iface = &o;
    7. //...
    8. exec();
    9. }
    10. private:
    11. QPointer<Object> m_iface;
    12. };
    To copy to clipboard, switch view to plain text mode 

    ... or the other way round (which is better as it doesn't force you to know when the thread starts):

    Qt Code:
    1. class Thread : public QThread {
    2. public:
    3. void setInterface(Object *o) { m_iface = o; }
    4. void run() {
    5. if(!m_iface) return; // refuse to run without the interface
    6. Internal i;
    7. connect(m_iface, SIGNAL(...), &i, SLOT(...)); // "m_iface" lives in the main thread, "i" lives in the worker thread
    8. connect(&i, SIGNAL(...), m_iface, SIGNAL(...)); // propagate signal outside
    9. //...
    10. exec();
    11. }
    12. private:
    13. QPointer<Object> m_iface;
    14. };
    To copy to clipboard, switch view to plain text mode 

    Of course in the second case the QThread object can serve as "m_iface".

  16. #14
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Deleting a thread content

    Quote Originally Posted by wysota View Post
    QStrings and QImages shouldn't be kept as pointers at all. Change them to regular objects and all will be fine. And you should never call QThread::terminate(). That's just a failsafe if you have to kill a thread and don't care about anything else.
    And regarding the use of pointers or regular objects, what would be the best way to load the image in case I want later to get that image and render it in my GUI thread? What I do now is use a pointer in my loader thread which does something like:

    Qt Code:
    1. image = new QImage(imageReader.read());
    To copy to clipboard, switch view to plain text mode 

    and then I show the image in my GUI thread in a label with:

    Qt Code:
    1. QPixmap pixmap = QPixmap::fromImage(loaderThread->image);
    2. label->setPixmap(pixmap);
    3. ...
    To copy to clipboard, switch view to plain text mode 

    As far as I can understand, this creates the QImage once in the read() method, then it creates another QImage in the heap and assigns the pointer. Then, again, I create the QPixmap which is again copied inside the label. Is this correct? Does this code really creates so many copies of the image? Would it be possible to do it quickly and smartly?
    Thanks again for your advices!

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    I can't think of any sane usecase where it would be necessary to allocate a QImage using the "new" operator. QImage is just like "int" - when would you allocate a single int using "new"?

  18. #16
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Deleting a thread content

    mmh... would you use a simple object QImage in the loader thread and then assign?
    Qt Code:
    1. image = imageReader.read();
    To copy to clipboard, switch view to plain text mode 
    my doubt is that I can start again the thread which should load another QImage. So, if in the following run of my thread I have another:
    Qt Code:
    1. image = imageReader.read();
    To copy to clipboard, switch view to plain text mode 
    is the previous image that was stored in the object deleted? I suppose this is a C++ doubt
    Thanks for your help!

  19. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Deleting a thread content

    Quote Originally Posted by Luc4 View Post
    mmh... would you use a simple object QImage in the loader thread and then assign?
    Yep.

    my doubt is that I can start again the thread which should load another QImage.
    It doesn't matter. QImage is an implicitly shared class. It will detach itself from all shared copies once you try to change its contents.

    is the previous image that was stored in the object deleted?
    It is "deleted" (not actually deleted but let's assume this is the case) if it was the only QImage object attached to the data contained in it. If there is another variable holding the same image, it will remain assigned to the other variable.

    I suggest you read a bit about "implicit sharing" in Qt - things will become clear then.

  20. The following user says thank you to wysota for this useful post:

    Luc4 (21st April 2010)

  21. #18
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Deleting a thread content

    This is awesome! Everything is clear now! Thanks for addressing me to that document!

Similar Threads

  1. Replies: 9
    Last Post: 28th November 2009, 20:31
  2. Replies: 16
    Last Post: 7th October 2009, 08:17
  3. Replies: 6
    Last Post: 29th April 2009, 18:17
  4. Replies: 2
    Last Post: 2nd August 2008, 10:16
  5. Replies: 1
    Last Post: 2nd August 2008, 10:05

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
  •  
Qt is a trademark of The Qt Company.