我使用此代码从 QTcpSocket 获取数据。我读取缓冲区中的数据,然后如果我立即释放它就可以了。但是如果我稍后释放它,应用程序就会崩溃。QtDocs 说我必须在使用方法 readBytes 后使用 delete[] 清理内存
void Widget::slotReadClient()
{
QTcpSocket* pClientSocket = (QTcpSocket*)sender();
QDataStream in(pClientSocket);
in.setVersion(QDataStream::Qt_4_7);
for (;;)
{
if (!m_nNextBlockSize)
{
if (pClientSocket->bytesAvailable() < sizeof(quint16))
{
break;
}
in >> m_nNextBlockSize;
}
if (pClientSocket->bytesAvailable() < m_nNextBlockSize)
{
break;
}
QString attribute;
quint16 count=0;
in>>count;//count of massives
quint16 els=count;
uint len=0;
char** wkbs;
if(count)
wkbs=new char*[els];
int j=0;
char* buf=0;
while(count)
{
in>>attribute;//description its ok
buf=wkbs[j];
buf=0;
in.readBytes(buf,len);
ui->plainTextEdit->appendPlainText(QString::number(len));//length of buffer ok
j++;
count--;
processGeom(buf);//data is OK
//delete [] buf; if I use this it works
}
ui->plainTextEdit->appendPlainText(QString::number(els));
j=0;
while(els)
{
buf=wkbs[j];
delete[] buf;//here I get crash
els--;
j++;
}
delete[] wkbs;
m_nNextBlockSize = 0;
}
}