我有一個名爲SFrame的結構,其中包含許多元素,值得注意的是2個無符號字符*類型的元素。我在我的類中創建了這個結構體的成員變量,但是我在我的類的某個函數的每次迭代中重新初始化它(除了某個布爾值爲true時)。我這樣做以下列方式:由於複製構造函數/刪除數組而導致內存損壞?
if (false == m_bRemainderNeedsProcessing)
{
// ... calls before and after the initialization are unimportant and not shown
m_sFrame = SFrame();
}
然後我通過m_sFrame到一個函數來分配一些其元素的,然後我需要在我的結構分配一個無符號字符陣列到我pszMessage變量。
m_sFrame.iMessageSize = m_sFrame.iPayloadLength;
m_sFrame.iOriginalMessageSize = m_sFrame.iPayloadLength;
m_sFrame.pszMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
m_sFrame.pszOriginalMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
這些SFrame實例存儲在S幀的載體,即
std::vector<SFrame>;
我希望能夠重用m_sFrame每次迭代,但我保證,如果我要清除內容的SFrame,當我將它存儲在矢量中時,SFrame被複制到矢量中而不會丟失它的指定值。爲此,我爲SFrame創建一個複製構造函數:
我附加了SFrame複製構造函數的一部分圖像。
ClearMemory(m_sFrame.pszMessage);
凡ClearMemory函數執行以下操作:
在我的函數結束時,我做了以下明確的pszMessage內存(並且是幾乎相同的pszOriginalMessage)
void CPCAPParser::ClearMemory(unsigned char *pszBuffer)
{
if(pszBuffer != NULL)
{
delete [] pszBuffer;
}
}
禰的問題是,這個功能似乎被刪除超過了它應該做的....因爲多次迭代後,我得到一個未處理的異常:訪問衝突......
我附上了幾張可能有助於傳達問題的圖像。真的需要這個:(幫助,如果有人需要我補充更多的細節讓我知道
感謝
http://imageshack.com/f/pduGDLGZp(常量:: RSSL_DECODE_PADDING長度爲7,有13個字節總數已設置 - 。在內存塊的開始)
http://imageshack.com/f/exRaaEmip取得明顯 - 當我打電話ClearMemory(內存地址顯然仍然是相同的)
我會發布更多的圖像,但我沒有足夠的代表。 ...
SFrame:
struct SFrame
{
int* ipTemp_int_ptr;
int* ipTemp_int_ptr_actual;
int* piTimestampPos;
int* piOffset;
int iIP_Header_Length;
int iTCP_Header_Length;
int iTCP_Source_Port;
int iTCP_Dest_Port;
long long uiSequenceNumber;
long long uiInitialSequenceNumber;
long long uiAckNumber;
int iIp_total_length;
int iActual_frame_length;
int iOriginal_frame_length;
int iCaptured_frame_length;
int iTotalPayloadLength;
int iTotalMsgLoad;
int iPayloadLength;
int iBytesComplete;
int iFragmentID;
int iRemainder;
int iMessageSize;
int iOriginalMessageSize;
long long iNextExpectedSequenceNum;
std::string strSourceAddress;
std::string strDestAddress;
std::string strTimestamp;
unsigned char* pszMessage;
unsigned char* pszOriginalMessage;
unsigned int uiClientID;
int iStartOfRemainder;
int iAccumulatedMsgLength;
SFrame() : ipTemp_int_ptr (NULL),
ipTemp_int_ptr_actual (NULL),
piTimestampPos (NULL),
piOffset (NULL),
pszMessage (NULL),
pszOriginalMessage (NULL),
iIP_Header_Length(0),
iTCP_Header_Length (0),
iTCP_Source_Port (0),
iTCP_Dest_Port (0),
iIp_total_length (0),
iActual_frame_length (0),
iOriginal_frame_length (0),
iCaptured_frame_length (0),
uiSequenceNumber(0),
uiInitialSequenceNumber (0),
uiAckNumber(0),
iPayloadLength (0),
iNextExpectedSequenceNum (0),
uiClientID (0),
iMessageSize (0),
iOriginalMessageSize (0),
iFragmentID(0),
iTotalPayloadLength(0),
iBytesComplete(0),
iAccumulatedMsgLength (0),
iRemainder (0),
iStartOfRemainder(0),
iTotalMsgLoad (0)
{
}
SFrame(const SFrame &c_rSrc)
{
*this = c_rSrc;
}
SFrame &SFrame::operator=(const SFrame &c_rSrc)
{
iIP_Header_Length = c_rSrc.iIP_Header_Length;
iTCP_Header_Length = c_rSrc.iTCP_Header_Length;
iTCP_Source_Port = c_rSrc.iTCP_Source_Port;
iTCP_Dest_Port = c_rSrc.iTCP_Dest_Port;
iIp_total_length = c_rSrc.iIp_total_length;
iActual_frame_length = c_rSrc.iActual_frame_length;
iOriginal_frame_length = c_rSrc.iOriginal_frame_length;
iCaptured_frame_length = c_rSrc.iCaptured_frame_length;
iPayloadLength = c_rSrc.iPayloadLength;
uiSequenceNumber = c_rSrc.uiSequenceNumber;
uiInitialSequenceNumber = c_rSrc.uiInitialSequenceNumber;
uiAckNumber = c_rSrc.uiAckNumber;
iNextExpectedSequenceNum = c_rSrc.iNextExpectedSequenceNum;
uiClientID = c_rSrc.uiClientID;
iFragmentID = c_rSrc.iFragmentID;
iMessageSize = c_rSrc.iMessageSize;
iOriginalMessageSize = c_rSrc.iOriginalMessageSize;
iTotalPayloadLength = c_rSrc.iTotalPayloadLength;
iBytesComplete = c_rSrc.iBytesComplete;
iAccumulatedMsgLength = c_rSrc.iAccumulatedMsgLength;
iRemainder = c_rSrc.iRemainder;
iStartOfRemainder = c_rSrc.iStartOfRemainder;
iTotalMsgLoad = c_rSrc.iTotalMsgLoad;
strSourceAddress = c_rSrc.strSourceAddress;
strDestAddress = c_rSrc.strDestAddress;
strTimestamp = c_rSrc.strTimestamp;
pszMessage = (c_rSrc.pszMessage == NULL) ? NULL : new unsigned char[c_rSrc.iMessageSize];
pszOriginalMessage = (c_rSrc.pszOriginalMessage == NULL) ? NULL : new unsigned char[c_rSrc.iOriginalMessageSize];
if(pszMessage != NULL)
{
memcpy(pszMessage, c_rSrc.pszMessage, c_rSrc.iMessageSize);
}
if(pszOriginalMessage != NULL)
{
memcpy(pszOriginalMessage, c_rSrc.pszOriginalMessage, c_rSrc.iOriginalMessageSize);
}
return *this;
}
~SFrame()
{
delete [] pszMessage;
delete [] pszOriginalMessage;
}
};
爲什麼這些成員指針而不是'std :: vector'? –
PaulMcKenzie
2014-08-27 23:15:44
http://imageshack.com/f/idjwk2pEp – 2014-08-27 23:16:05
哪位成員Paul?... – 2014-08-27 23:17:19