使用此代碼從QTcpSocket獲取數據。我在緩衝區中讀取數據,然後立即釋放數據。但是如果我稍後釋放它,應用程序就會崩潰。 QtDocs說,我要清理內存刪除[]使用方法的ReadBytes數據緩衝區
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;
}
}
非常感謝你的解釋 – bossman 2012-07-14 11:29:54