为什么下面的程序,free就不能用呢
注释去掉,就报错,为什么??
BOOL CMainFrame::CFGDbSelect(CString sql,CTSData* tsdata)
{
char* temp;
int length = sql.GetLength();
if( length == 0 )
{
return FALSE;
} temp = (char *)malloc(length+1);
memset(temp, 0 , length+1);
strcpy(temp,sql); COnlyHead oHead;
oHead.iAddStrLen = strlen(temp);
oHead.sAddStr = temp;
oHead.SetPacketHead(P_COM_PKT_DBSELECT,
P_COM_SV_RET,
sizeof(PacketHead) + strlen(temp),
(unsigned short)1,
strlen(temp),
'0');
// 发送包
if( !Send(&oHead) )
{
TelComMessage("E0000002");
return FALSE;
}
// 接收查询应答包
if( !RecvPacketHead(&oHead) )
{
TelComMessage("E0000003");
return FALSE;
}
if( oHead.m_pHead->m_stPacketHead.cRetCode != TELCOM_OK )
{
TelComMessage("E0000003");
return FALSE;
}
long lLen = TSstoi(oHead.m_pHead->m_stPacketHead.slPacketSize) - sizeof(PacketHead);
if( !RecvString(&oHead, lLen) )
{
TelComMessage("E0000003");
return FALSE;
} tsdata->ImportXML(oHead.sAddStr,lLen); //free( temp );
//temp = NULL;
return TRUE;
}
注释去掉,就报错,为什么??
BOOL CMainFrame::CFGDbSelect(CString sql,CTSData* tsdata)
{
char* temp;
int length = sql.GetLength();
if( length == 0 )
{
return FALSE;
} temp = (char *)malloc(length+1);
memset(temp, 0 , length+1);
strcpy(temp,sql); COnlyHead oHead;
oHead.iAddStrLen = strlen(temp);
oHead.sAddStr = temp;
oHead.SetPacketHead(P_COM_PKT_DBSELECT,
P_COM_SV_RET,
sizeof(PacketHead) + strlen(temp),
(unsigned short)1,
strlen(temp),
'0');
// 发送包
if( !Send(&oHead) )
{
TelComMessage("E0000002");
return FALSE;
}
// 接收查询应答包
if( !RecvPacketHead(&oHead) )
{
TelComMessage("E0000003");
return FALSE;
}
if( oHead.m_pHead->m_stPacketHead.cRetCode != TELCOM_OK )
{
TelComMessage("E0000003");
return FALSE;
}
long lLen = TSstoi(oHead.m_pHead->m_stPacketHead.slPacketSize) - sizeof(PacketHead);
if( !RecvString(&oHead, lLen) )
{
TelComMessage("E0000003");
return FALSE;
} tsdata->ImportXML(oHead.sAddStr,lLen); //free( temp );
//temp = NULL;
return TRUE;
}
2、free(temp)改为:
if(temp)
{
free(temp);
temp = NULL;
}
oHead.iAddStrLen = strlen(temp);
oHead.sAddStr = temp;COnlyHead的析构函数有没有删除sAddStr的操作,如果有那就不对了
其它感觉好象没什么问题。
if(temp != NULL)
{
free(temp);
temp = NULL;
}
http://www.libsdl.org/pipermail/sdl/1999-April/020636.htmlCOnlyHead不是我写的,我也不知道里面怎么搞得