Results 1 to 4 of 4

Thread: QThread new and delete

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QThread new and delete

    Hi
    Is it legal to delete a memory which is allocated( using new ) in another thread.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread new and delete

    Sometimes yes, sometimes not. Your question is way too generic to be able to give a specific answer.
    J-P Nurmi

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QThread new and delete

    Sometimes yes, sometimes not. Your question is way too generic to be able to give a specific answer.
    I have a class like this
    Qt Code:
    1. class PlotData
    2. {
    3. private :
    4. double *data;
    5.  
    6. public:
    7. PlotData()
    8. {
    9. data = new double[1000];
    10. }
    11. ~PlotData()
    12. {
    13. delete []data;
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 

    and a class which inherits QThread
    Qt Code:
    1. class Thread:QThread
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. signals:
    7. void signal_process(PlotData*);
    8.  
    9. public:
    10. void run()
    11. {
    12. while(true)
    13. {
    14. PlotData *pd = new PlotData();
    15. emit signal_process(pd);
    16. }
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 

    now in the main gui thread i have a slot like void Delete_PlotData(PlotData*) .
    Qt Code:
    1. connect(&thread,SIGNAL(signal_process(PlotData*)),this,Delete_PlotData(PlotData*)));
    To copy to clipboard, switch view to plain text mode 

    Now is it legal to call the PlotData destructor as below.
    Qt Code:
    1. void Delete_PlotData(PlotData* pd)
    2. {
    3. pd->~PlotData() //Is this legal
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread new and delete

    Looks valid to me since it's just a block of data, not a QObject for example. Alternatively you could make the data structure implicitly shared with help of QSharedData and QSharedDataPointer, pass it by reference and stop worrying about memory management...
    J-P Nurmi

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

    babu198649 (19th April 2009)

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.