我有一个功能。在这里我试图将结构复制到另一个相同类型的结构。此结构存储为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;
};
这是调用堆栈:当我复制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
答案 0 :(得分:2)
在复制运行之前,您可能有一些先前的损坏。被删除的指针 - 0xCDCDCDCD - 是可疑的&#34;常规&#34;,这表明它可能是编译器以某种方式放置在未初始化或已经释放的内存中的标记值(这里有这样的历史记录)值,最着名的可能是0xDEADBEEF)。
pProjectdata->m_emuComputerData
变量可能没有正确初始化,或者已经被破坏了。 CProjectData
类似乎可疑... AddBuffer
似乎不是类型感知的,这意味着CProjectData
的构造函数没有运行;使用std::string
或strcpy_s
sizeof m_szProjectName
,会比MAX_PROJECTNAME_LENGTH + 1
FWIW,vector
s可以用&#34;值语义复制&#34;只要它们的内容也可以,所以这段代码:
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);
}
可以简化为:
pProjectdata->m_emuComputerData.push_back(m_emuComputerData);