View Full Version : QThread new and delete
babu198649
18th April 2009, 10:37
Hi
Is it legal to delete a memory which is allocated( using new ) in another thread.
jpn
18th April 2009, 11:59
Sometimes yes, sometimes not. Your question is way too generic to be able to give a specific answer.
babu198649
18th April 2009, 13:04
Sometimes yes, sometimes not. Your question is way too generic to be able to give a specific answer.
I have a class like this
class PlotData
{
private :
double *data;
public:
PlotData()
{
data = new double[1000];
}
~PlotData()
{
delete []data;
}
};
and a class which inherits QThread
class Thread:QThread
{
Q_OBJECT
signals:
void signal_process(PlotData*);
public:
void run()
{
while(true)
{
PlotData *pd = new PlotData();
emit signal_process(pd);
}
}
};
now in the main gui thread i have a slot like void Delete_PlotData(PlotData*) .
connect(&thread,SIGNAL(signal_process(PlotData*)),this,Dele te_PlotData(PlotData*)));
Now is it legal to call the PlotData destructor as below.
void Delete_PlotData(PlotData* pd)
{
pd->~PlotData() //Is this legal
}
jpn
18th April 2009, 14:22
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... ;)
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.