我有一個函數。在這我試圖複製一個結構到另一個相同類型的結構。這個結構作爲向量存儲在CProjectData
類中。 當我嘗試複製內容時,它崩潰。的代碼段如下所示:將矢量複製到另一個時發生崩潰
void CProjectData::send()
{
struct EmuComputerData tempStruct;
//ignore this line
m_pClient->GetTeleSend()->CreateHeader(GetFbMsgType(m_dwCmdId));
//ignore this line
CProjectData* pProjectdata = (CProjectData*) m_pClient->GetTeleSend()->AddBuffer(sizeof(CProjectData));
//ignore this line
pProjectdata->m_wErrorCode = m_wErrorCode;
//ignore this line
strcpy_s(pProjectdata->m_szProjectName, MAX_PROJECTNAME_LENGTH + 1, m_szProjectName);
vector<struct EmuComputerData>::iterator iter;
iter = m_emuComputerData.begin();
for (; iter != m_emuComputerData.end(); iter++)
{
tempStruct.wEmuComputerNo = (*iter).wEmuComputerNo;
tempStruct.dwIPAddress = (*iter).dwIPAddress;
tempStruct.wPCCount = (*iter).wPCCount;
vector<WORD>::iterator apIter;
apIter = (*iter).apNumbers.begin();
for (; apIter != (*iter).apNumbers.end(); apIter++)
{
tempStruct.apNumbers.push_back(*apIter);
}
//crashes after executing this line
pProjectdata->m_emuComputerData.push_back(tempStruct);
}
}
m_emuComputerData
被聲明爲
vector<struct EmuComputerData> m_emuComputerData;
在ProjectData
類。
這是一個結構,它又包含一個向量。
struct EmuComputerData
{
WORD wPCCount;
WORD wEmuComputerNo;
DWORD dwIPAddress;
vector<WORD> apNumbers;
};
這裏是調用堆棧:它崩潰,當我TRU複製vector<WORD> apNumbers
。我相信我在複製這個矢量的時候正在做一些愚蠢的事情。
msvcr90d.dll!operator delete(void * pUserData=0xcdcdcdcd) Line 52 + 0x3 bytes C++
EmuDiagnosticsService.exe!std::allocator<EmuComputerData>::deallocate(EmuComputerData * _Ptr=0xcdcdcdcd, unsigned int __formal=0) Line 140 + 0x9 bytes C++
EmuDiagnosticsService.exe!std::vector<EmuComputerData,std::allocator<EmuComputerData> >::_Insert_n(std::_Vector_const_iterator<EmuComputerData,std::allocator<EmuComputerData> > _Where={wPCCount=??? wEmuComputerNo=??? dwIPAddress=??? ...}, unsigned int _Count=1, const EmuComputerData & _Val={...}) Line 1202 C++
EmuDiagnosticsService.exe!std::vector<EmuComputerData,std::allocator<EmuComputerData> >::insert(std::_Vector_const_iterator<EmuComputerData,std::allocator<EmuComputerData> > _Where={wPCCount=??? wEmuComputerNo=??? dwIPAddress=??? ...}, const EmuComputerData & _Val={...}) Line 878 C++
EmuDiagnosticsService.exe!std::vector<EmuComputerData,std::allocator<EmuComputerData> >::push_back(const EmuComputerData & _Val={...}) Line 823 + 0x58 bytes C++
> EmuDiagnosticsService.exe!CProjectData::send() Line 574 C++
EmuDiagnosticsService.exe!CEmuDiagTCPServer::Send() Line 374 + 0xf bytes C++
EmuDiagnosticsService.exe!StartTCPServer(void * dummy=0x00000000) Line 251 C++
感謝
Nipun
是否有可能'pProjectdata'實際上是'this'?這意味着你正在推進你正在閱讀的同一個矢量,這將導致麻煩 –