经shiroki的指正,其实QT有更好的机制来释放对象.那就是deleteLater(). 于是,事情就显得非常简单,请看代码:
- UdpSocketThread::UdpSocketThread(QObject *parent) :
- QThread(parent)
- {
- socket = 0;
- }
- UdpSocketThread::~UdpSocketThread()
- {
- this->socket->deleteLater();
- quit();
- wait();
- }
- void UdpSocketThread::run()
- {
- socket = new QUdpSocket;
- connect(socket,SIGNAL(readyRead()),this,SLOT(readPendingDatagrams()));
- socket->bind(2234);
- exec();
- }