Results 1 to 3 of 3

Thread: Freeing a QProcess after it has finished using deleteLater()

  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 Freeing a QProcess after it has finished using deleteLater()

    Hi! I usually do this to free a QProcess (or a QThread after it has finished):

    Qt Code:
    1. QProcess* proc = new QProcess();
    2. connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 

    and seems to work just fine, and I suppose is the correct way of freeing memory. Am I right?

    Now, I find myself in the situation where I need to do something after the process has finished, but I also want to delete the pointer. Can I do this?

    Qt Code:
    1. QProcess* proc = new QProcess();
    2. connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
    3. connect(proc, SIGNAL(finished(int)), <some_pointer>, SLOT(doSomething()));
    To copy to clipboard, switch view to plain text mode 

    I tried this and it seems to work but I don't know if it is the correct way to go. If it isn't, what is the best solution?
    Thanks!

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Freeing a QProcess after it has finished using deleteLater()

    it is safe since deleteLater will have no effect until event loop kicks in (new event loops have no effect).
    Note that by default slots are called synchronously, so before event loop start processing new events.
    In general you should remember that order of calling slots (when invoke by signals) is undefined (you can't assume that you have a control who will get signal first).

  3. The following user says thank you to MarekR22 for this useful post:

    Luc4 (13th July 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Freeing a QProcess after it has finished using deleteLater()

    Quote Originally Posted by Luc4
    I tried this and it seems to work but I don't know if it is the correct way to go. If it isn't, what is the best solution?
    It is safe to use, the way you mentioned, but it would more logical to connect the deleteLater() as the last connection, like this

    Qt Code:
    1. QProcess* proc = new QProcess();
    2. connect(proc, SIGNAL(finished(int)), <some_pointer_1>, SLOT(doSomething1()));
    3. connect(proc, SIGNAL(finished(int)), <some_pointer_2>, SLOT(doSomething2()));
    4. connect(proc, SIGNAL(finished(int)), <some_pointer_3>, SLOT(doSomething3()));
    5. connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by MarekR22
    In general you should remember that order of calling slots (when invoke by signals) is undefined (you can't assume that you have a control who will get signal first).
    If a signal is connected to several slots, the slots are activated in the same order as the order the connection was made, when the signal is emitted.

  5. The following user says thank you to Santosh Reddy for this useful post:

    Luc4 (13th July 2011)

Similar Threads

  1. QProcess finished slot not firing
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2010, 13:09
  2. QProcess::finished problems again
    By mdicosimo in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2009, 21:43
  3. QProcess object not emitting finished(int) signal
    By Tiansen in forum Qt Programming
    Replies: 13
    Last Post: 14th November 2008, 12:17
  4. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  5. QProcess::finished()
    By T1c4L in forum Qt Programming
    Replies: 11
    Last Post: 9th July 2008, 20:06

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.